class A { static function f() { class B { static function g() { echo __METHOD__; } } B::g(); } }
class A { static function f() { class B { static function g() { echo __CLASS__; } } B::g(); } }
class A { static function f() { class B { static function g() { echo __FUNCTION__; } } B::g(); } }
{ print "A::baz\n"; } private function f() { echo "A::f\n"; } public function g($a) { echo "A::g\n"; $a->f(); } } class B extends A { protected function bar() { print "B::bar\n"; $this->baz(); } public function h($a) { print "B::g\n"; $a->f(); } } $a = new A(); $b = new B(); $b->foo(); $b->g($a); #$b->h($a);
<?php class A { function f() : int { return 42; } } class B { function g() : string { return 'string'; } } $a = new A(); print $a->g(); print $a->f(); $a = new B(); print $a->g(); print $a->f();
<?php class A { static function f() { return new static(); } static function g($o) { return $o instanceof static; } } class B extends A { } var_dump(A::g(A::f())); var_dump(A::g(B::f())); var_dump(B::g(A::f())); var_dump(B::g(B::f()));
<?php class A { function f() { } } class B { function g() { } } $v = new A(); if (rand(0, 1) > 0) { $v = new B(); $v->g(); } else { $v->f(); } if (rand(0, 1) > 0) { $v = new A(); $v->g(); } else { $v = new B(); $v->f(); }