function main1() { $obj = new C1(); // FPushObjMethodD $obj->__call("a", "b", "c", "d"); $obj->foo("a", "b", "c", "d"); // FPushObjMethod $fn = '__call'; $obj->{$fn}("a", "b", "c", "d"); $fn = 'foo'; $obj->{$fn}("a", "b", "c", "d"); }
public function f() { $v = function () { return parent::f(); }; return $v(); }
public function test() { C1::__invoke(0, 1); D1::__invoke(0, 1); E1::__invoke(0, 1); call_user_func(mk('C1'), 0, 1); call_user_func(mk('D1'), 0, 1); call_user_func(mk('E1'), 0, 1); }
function main() { $o = new C1(); // First pass through, "learn" that these calls go to C1::__call. for ($i = 0; $i < 12; $i++) { $o->maul(1, 2, $i, 3); $o->maul(1, 2, $i, 3, 4); $o->maul(1, 2, $i, 3, 4, 5); $o->maul(1, 2, $i, 3, 4, 5, 6); $o->maul(1, 2, $i, 3, 4, 5, 6, 7); $o->maul(1, 2, $i, 3, 4, 5, 6, 7, 8); // Send subsequent passes to C2::__call $o = new C2(); } }
function main1() { // FPushClsMethodD C1::__callStatic("a", "b", "c", "d"); C1::foo("a", "b", "c", "d"); // FPushClsMethod $cls = 'C1'; $cls::__callStatic("a", "b", "c", "d"); $cls::foo("a", "b", "c", "d"); $fn = '__callStatic'; C1::$fn("a", "b", "c", "d"); $fn = 'foo'; C1::$fn("a", "b", "c", "d"); $fn = '__callStatic'; $cls::$fn("a", "b", "c", "d"); $fn = 'foo'; $cls::$fn("a", "b", "c", "d"); }
public function g() { parent::f(); }
<?php trait T { function info() { (yield __METHOD__); (yield __CLASS__); (yield __TRAIT__); } } class C1 { use T; } class C2 { use T; } $o1 = new C1(); foreach ($o1->info() as $info) { var_dump($info); } $o2 = new C2(); foreach ($o2->info() as $info) { var_dump($info); }
} public function test() { // This should call C1::foo, not D1::foo $obj = new D1(); $obj->foo(); } } class D1 extends C1 { private function foo() { echo "D1::foo\n"; } } $obj = new C1(); $obj->test(); class C2 { private function foo() { echo "C2::foo\n"; } public function test() { $this->foo(); } } class D2 extends C2 { protected function foo()
trait T1 { function fruit() { (yield 'apple'); (yield 'banana'); } } trait T2 { function fruit() { (yield 'pear'); (yield 'grape'); } } class C1 { use T1, T2 { T1::fruit insteadof T2; T2::fruit as fruit2; } } $o = new C1(); foreach ($o->fruit() as $fruit) { var_dump($fruit); } foreach ($o->fruit2() as $fruit) { var_dump($fruit); }
{ protected abstract function gpc(); public function gen() { (yield $this->gpc()); } } class C1 { use T; protected function gpc() { return 1; } } class C2 { use T; protected function gpc() { return 2; } } $obj1 = new C1(); $obj2 = new C2(); $c1 = $obj1->gen(); $c2 = $obj2->gen(); $c1->next(); var_dump($c1->current()); $c2->next(); var_dump($c2->current());
static function f1() { $c = parent::make(); $c->p = 2; }
} } class C2 { function &__invoke(&$a) { return $a; } } class C3 { function __invoke() { } } $x = new C1(); var_dump($x()); var_dump($x->__invoke()); $x(); $x->__invoke(); $x = function () { return 0; }; var_dump($x()); var_dump($x->__invoke()); $x(); $x->__invoke(); $x = new C2(); $a = $b = $c = $d = 1; $e =& $x($a); $e = 2;
echo "<C8::f1>\n"; var_dump($this->a1); echo "</C8::f1>\n"; } function __get($var) { echo "C8::__get({$var})\n"; return $this->g($var); } function g($var) { echo "C8::g({$var})\n"; return $this->{$var}; } } $c1 = new C1(); $c1->f1(); var_dump($a1); $c1->f2(); $c1->f3(); $c2 = new C2(); $c2->f1(); $c3 = new C3(); $c3->f1(); $c4 = new C4(); $c4->f1(); $c5 = new C5(); $c5->f1(); $c6 = new C6(); $c6->f1(); $c7 = new C7();
public function __invoke($a0) { return $a0 > 10; } } $c = new C1(); $d = new D1(); $e = new E1(); $c(0, 1); $d(0, 1); $e(0, 1); call_user_func($c, 0, 1); call_user_func($d, 0, 1); call_user_func($e, 0, 1); call_user_func_array($c, array(0, 1)); call_user_func_array($d, array(0, 1)); call_user_func_array($e, array(0, 1)); $c->__invoke(0, 1); $d->__invoke(0, 1); $e->__invoke(0, 1); C1::__invoke(0, 1); D1::__invoke(0, 1); E1::__invoke(0, 1); function mk($n) { return $n . '::__invoke'; } call_user_func(mk('C1'), 0, 1); call_user_func(mk('D1'), 0, 1); call_user_func(mk('E1'), 0, 1); var_dump(array_filter(array(0, 1, 11, 13), new F1()));
/** @param NotFound $p */ public function __construct($p) { parent::__construct($p); }
{ use Counter; } class C2 { use WithConstructor; } // trait test method (new C())->test(); (new C())->newTest(); // 42 echo (new C())->h . PHP_EOL; // fatal: call to protected method //(new C())->pub(); // works (new C())->newPriv(); (new C())->abs(); // works (new C())->getPrivateFromTrait(); // notice: undefined (new C())->getPrivateFromClass(); $c = new C(); $c->incr(); $c->incr(); // 2 $c1 = new C1(); $c1->incr(); // 1 $c2 = new C2(); // trait constructor echo PHP_EOL;
// define a level-1 namespace echo "Inside namespace " . __NAMESPACE__ . "\n"; use NS2; namespace NS3\Sub1; // define a level-2 namespace who's prefix is not an existing level-1 ns echo "Inside namespace " . __NAMESPACE__ . "\n"; //class NS3\Sub1\C1 // prefix not allowed in definition class C1 { // const NS3\Sub1\CON = 123; // prefix not allowed in definition const CON = 123; // public function NS3\Sub1\f() // prefix not allowed in definition public function f() { echo "Inside function " . __FUNCTION__ . "\n"; echo "Inside method " . __METHOD__ . "\n"; } } $c1 = new C1(); $c1->f(); interface I1 { } //Interface NS3\Sub1\I1 {} // prefix not allowed in definition /* namespace NS10 { // Cannot mix bracketed namespace declarations with unbracketed namespace declarations echo "Inside namespace " . __NAMESPACE__ . "\n"; } */
function f() { parent::f(); }
<?php trait T1 { function m1() { echo "T:m1\n"; } function m2() { echo "T:m2\n"; } } class C1 { use T1 { m1 as a1; } } $o = new C1(); $o->m1(); $o->a1(); $o->m2(); $o->a2();
{ echo "B1::bar\n"; } public function baz() { $this->bar(); } } class C1 extends B1 { protected function bar() { echo "C1::bar\n"; } } $obj = new C1(); $obj->baz(); class A2 { private function bar() { echo "A2::bar\n"; } } class B2 extends A2 { public function bar() { echo "B2::bar\n"; } public function baz()
<?php class A1 { public function A1($id) { $this->id = $id; } } class B1 extends A1 { } class C1 extends B1 { public function __construct($id) { parent::__construct($id); } function zz($id) { parent::__construct($id); } } $x = new C1(100); echo $x->id . "\n"; $x->zz(1); echo $x->id . "\n";
--TEST-- Statics should work in traits, too. --FILE-- <?php error_reporting(E_ALL); trait Counter { public function inc() { static $c = 0; $c = $c + 1; echo "{$c}\n"; } } class C1 { use Counter; } $o = new C1(); $o->inc(); $o->inc(); ?> --EXPECTF-- 1 2
{ public static $x = 1; public function printX() { var_dump(self::$x); } } class C1 { use T; } class C2 { use T; } $o1 = new C1(); $o2 = new C2(); var_dump(T::$x); var_dump(C1::$x); var_dump(C2::$x); $o1->printX(); $o2->printX(); T::$x++; var_dump(T::$x); var_dump(C1::$x); var_dump(C2::$x); $o1->printX(); $o2->printX(); C1::$x++; var_dump(T::$x); var_dump(C1::$x);