<?php X::test(); /** Class X is related to neither ParentClass nor ChildClass. */ class X { public static function test() { $myChild = new ChildClass(); $myChild->secret(); // bug - invokes X::secret() instead of ChildClass::secret() } private function secret() { echo "Called private " . __METHOD__ . "() on an instance of: " . get_class($this) . "\n"; } } class ParentClass { private function secret() { } } class ChildClass extends ParentClass { public function secret() { echo "Called public " . __METHOD__ . "() on an instance of: " . get_class($this) . "\n"; } }
<?php class X { function test(self $s) { var_dump($s); } } X::test("hello");
<?php class X { static function test() { $f = function () { get_called_class(); }; $g = $f->bindto(null, null); return $g; } } $f = X::test(); $f();
<?php class X { static function test() { var_dump(__FUNCTION__); var_dump(__CLASS__); var_dump(__METHOD__); return array($GLOBALS[__FUNCTION__], $GLOBALS[__CLASS__], $GLOBALS[__METHOD__]); } } $test = 'this_is_function_test'; $X = 'this_is_class_x'; $GLOBALS['X::test'] = 'this_is_method_test::x'; var_dump(X::test());
} } class C extends B { private $hiddenPriv = 'C::hiddenPriv'; public static function test($b) { echo __METHOD__ . "\n"; var_dump(get_object_vars($b)); } } class X { public static function test($b) { echo __METHOD__ . "\n"; var_dump(get_object_vars($b)); } } $b = new B(); echo "\n---( Global scope: )---\n"; var_dump(get_object_vars($b)); echo "\n---( Declaring class: )---\n"; B::test($b); echo "\n---( Subclass: )---\n"; C::test($b); echo "\n---( Superclass: )---\n"; A::test($b); echo "\n---( Unrelated class: )---\n"; X::test($b);
function test($a) { $x = new X(); return $a ? $x->test(1, 2) : false; }
<?php class X { public function __invoke($x) { var_dump($x); } public function test() { $this(10); call_user_func($this, 300); call_user_func_array($this, array(0, 1)); } } class Y { public function test($x) { $x(10); call_user_func($x, 300); call_user_func_array($x, array(0, 1)); } } $x = new X(); $x->test(); $y = new Y(); $y->test($x);
<?php class X { public $foo; function test() { $a = array(null => $this->foo); return $a; } } $x = new X(); var_dump($x->test());