php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50418 self in closures has no class scope
Submitted: 2009-12-08 23:55 UTC Modified: 2009-12-09 08:37 UTC
From: crashrox at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.1 OS: Linux
Private report: No CVE-ID: None
 [2009-12-08 23:55 UTC] crashrox at gmail dot com
Description:
------------
A closure within a static method loses self scope. When you print __CLASS__ within a closure it claims to belong to the class. But yet you cant access anything from that class.

Reproduce code:
---------------
class Test {
	public  function do_something() {
		$func = function() {
			echo 'I belong to: ' . __CLASS__;
			return self::do_something_else();
		};
		
		return $func();
	}
	
	public static function do_something_else() {
		return 'I did something else';
	}
}

echo Test::do_something();

Expected result:
----------------
i belong to: Test
i did something else

Actual result:
--------------
Test
Fatal error: Cannot access self:: when no class scope is active in /home/jument/adam/jument/web/index.php on line 7

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-09 08:37 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Of course __CLASS__ shows the current class you're "in". It's a constant, available in all scopes. 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 08:01:36 2025 UTC