<?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());
<?php class Foo { protected $unsetme = 1; protected $keepme = 2; public function test() { $a = get_object_vars($this); foreach ($a as $k => $v) { if ($k == 'unsetme') { echo "Unsetting: {$k}\n"; unset($a[$k]); } else { if ($k == 'keepme') { echo "Changing: {$k}\n"; $a[$k] = 42; $a['keepme'] = 43; } } } var_dump($a, array_keys($a)); } } $f = new Foo(); $f->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';
public static function test4() { parent::test(); // Gives Foo, even though we're calling from a child }
public function test($a) { parent::test($a); }
[expect php] [file] <?php require 'Phalanger.inc'; define('TEN', 10); class Foo { const HUN = 100; function test($x = Foo::HUN) { static $arr2 = array(TEN => 'ten'); static $arr = array(Foo::HUN => 'ten'); __var_dump($arr); __var_dump($arr2); __var_dump($x); } } @Foo::test(); echo Foo::HUN . "\n";
<?php class Foo { public static function test() { throw new Exception('a'); return 'a'; } } try { $a = Foo::test(); } catch (Exception $e) { echo 'b'; } var_dump($a);
--TEST-- Bug #138 --FILE-- <?php class Foo { var $bla = 'fail'; function test() { //against dead code optimization (=, call func, return and etc.) $var = function () { if (true) { //or other code blocks (if, switch-case, while, for) //against dead code optimization $var = function () { $this->bla = 'fooo'; }; $var(); } }; $var(); } } $foo = new Foo(); $foo->test(); var_dump($foo->bla); ?> --EXPECT-- string(4) "fooo"
<?php class Foo { public function __call($a, $b) { print "nonstatic\n"; var_dump($a); } public static function __callStatic($a, $b) { print "static\n"; var_dump($a); } public function test() { $this->fOoBaR(); self::foOBAr(); $this::fOOBAr(); } } $a = new Foo(); $a->test(); $a::bAr(); foo::BAZ();