class Foo { public function __construct() { Stat::getInstance(); } } class Error { private $trace; public function __construct() { $this->trace = debug_backtrace(1); } } class Bar { public function __destruct() { Stat::getInstance(); new Error(); } public function test() { new Error(); } } $foo = new Foo(); $bar = new Bar(); $bar->test();
<?php require __DIR__ . "/classes.php.inc"; class Foo { public static function test() : A { return new A(); } } class Bar extends Foo { public static function test() : A { return new B(); } } var_dump(Bar::test()); var_dump(Foo::test());
} public static function test2() { $a = new static(); // Gives Bar, the 'late static binding' class var_dump($a); } } class Bar extends Foo { public static function test3() { $a = new self(); var_dump($a); // Gives Bar, this class } public static function test4() { parent::test(); // Gives Foo, even though we're calling from a child } public static function test5() { parent::test2(); // Gives Bar, because its using the late static binding context } } $obj = Bar::test(); $obj = Bar::test3(); $obj = Bar::test4(); $obj = Bar::test5();
function main() { $bar = new Bar(); echo $bar->test(); }
} class Simple { function test() { static $i; $i++; return $i; } } test(); test(); Foo::test(); Foo::test(); Bar::test(); Bar::test(); $simple = new Simple(); $simple->test(); $simple->test(); static $one = 20, $two, $three = 40; if ($one !== 20) { return 'fail_1'; } if ($two !== null) { return 'fail_2'; } if ($three !== 40) { return 'fail_3'; } return test() + Foo::test() + Bar::test() + $simple->test() === 12 ? 'success' : 'fail';
<?php class Foo { static function call() { return 'fail'; } static function test() { return static::call(); } static function get() { return new static(); } } class Bar extends Foo { static function call() { return 'success'; } } $bar = Bar::get(); if (!$bar instanceof Bar) { return 'fail_get'; } return Bar::test();