function test() { $a = X::foo(); $a(); $a = Y::foo(); $a(); $a = X::bar(); $a(); $a = Y::bar(); $a(); $x = new X(); $a = $x->bar(); $a(); $x = new Y(); $a = $x->bar(); $a(); $a = X::bar_nonstatic(); $a(); $a = Y::bar_nonstatic(); $a(); $x = new X(); $a = $x->bar_nonstatic(); $a(); $x = new Y(); $a = $x->bar_nonstatic(); $a(); }
function test($str) { X::bar($str); (new X())->bar($str); Y::bar($str); (new Y())->bar($str); }
function foo($x) { global $f; if ($x == 0) { echo "statically known invoke\n"; $g = function ($x) { var_dump(__METHOD__); }; $g::__invoke(f()); } else { if ($x == 1) { echo "call_user_func\n"; call_user_func(array(get_class($f), "__invoke"), f()); } else { echo "X::bar\n"; X::bar(); echo "cuf(X::bar)\n"; call_user_func(array('X', 'bar')); echo "statically unknown invoke\n"; $f::__invoke(f()); } } }
[expect php] [file] <?php class X { public function __call($name, $args) { var_dump($name, $args); return 123; } protected function bar2($arg) { echo "bar2" . $arg; } public function bar() { return $this->bar2("hello", 1, 2, 3, 4, 5, 6, 7, 8, 9) . $this->bar2("hello") . $this->bar3("hello", 1, 2, 3, 4, 5, 6, 7, 8, 9); } } $x = new X(); $x->bar();
} class Z extends Y { public function zoo2() { return "zoo2/" . $this->nonexistingzoo(8, 8, 8); } public function zoo() { return $this->bar() . $this->zoo2() . X::nonexisting1(3); } } // calling static methods resolved in compile time echo X::nonexistingfoo(1, 2, 3); echo call_user_func_array(array("X", "nonexisting2"), array(10, 20, 30)); echo Y::nonexistingfoo(1, 2, 3); echo Y::nonexistingfoo2(4); echo call_user_func_array(array("Y", "nonexisting3"), array("a", 'b', 'c')); echo Z::nonexistingstatic(); // calling instance methods $x = new X(); echo $x->bar(); echo $x->non_existing_func(1, 2, 3); echo call_user_func(array($x, "foo"), 5, 6, 7); $y = new Y(); echo $y->bar(); echo $y->nonexisting4(9, 8, 7); echo call_user_func(array($y, "bar"), 4, 5, 6); echo call_user_func(array($y, "nonexisting5"), 333, 666, 999); $z = new Z(); echo $z->zoo();
<?php class X { static function foo() { return false; } static function bar() { return 5.5; } static function baz() { return time(); } } var_dump(X::foo()); var_dump(X::bar()); var_dump(gettype(X::baz()));
function test() { X::bar(null); }
function baz() { X::bar(); }
} } function bar($arg) { switch ($this) { case $arg: echo 'arg'; break; default: echo 'def'; } } function baz($arg) { switch ($this) { case $arg: echo 'arg'; break; default: echo 'def'; } (yield $arg); } } $x = new X(); $x->foo(); $x->bar(new stdClass()); $x->bar($x); foreach ($x->baz($x) as $v) { var_dump($v); }
<?php function foobar() { echo "foobar\n"; } class X { static function foo() { echo "X::foo\n"; } function bar() { echo "Y::bar\n"; } } $y = new X(); foobar(); X::foo(); $y->bar();