<?php class A { function foo() { if (isset($this)) { echo '$this está definida ('; echo get_class($this); echo ")\n"; } else { echo "\$this no está definida.\n"; } } } class B { function bar() { // Nota: la siguiente línea arrojará una advertencia si E_STRICT está habilitado. A::foo(); } } $a = new A(); $a->foo(); // Nota: la siguiente línea arrojará una advertencia si E_STRICT está habilitado. A::foo(); $b = new B(); $b->bar(); // Nota: la siguiente línea arrojará una advertencia si E_STRICT está habilitado. B::bar();
class A { private $a = 0; protected $b = 0; public $c = 0; function foo() { var_dump($this->a, $this->b, $this->c); } function bar() { echo __METHOD__ . "\n"; var_dump($this->a, $this->b, $this->c); } } class B extends A { public $a = 1; public $b = 1; public $c = 1; function foo() { var_dump($this->a, $this->b, $this->c); } } $x = new A(); $x->foo(); $x = new B(); $x->foo(); $x->bar();
<?php class A { public function foo() { echo "A::foo()\n"; } } class B extends A { public function __call($f, $args) { echo "B::__call({$f}, {$args})\n"; } } $o = new B(); // this will call A::foo. The __call is used only when // nothing was found in the whole hierarchy, not just // when nothing was found in your class. $o->foo(); // this will call B::__call $o->bar();
function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } } } class B { function bar() { A::foo(); } } $a = new A(); $a->foo(); // foo是普通方法,$a绑定到$this,因此,$this is defined(A); A::foo(); // 这样调用,是不规范的.$this is not defined $b = new B(); $b->bar(); // bar是普通方法,$b绑定到$this,bar(){A::foo-这里是静态调用,不操作$this} // $this is defined (B); B::bar(); // this is not defined ?>
public function doBar() { $this->bar(); // G G B::bar(); // B B C::bar(); // C C D::bar(); // D D F::bar(); // F F G::bar(); // G G H::bar(); // H H parent::bar(); // F G self::bar(); // G G static::bar(); // G G echo "****************\n"; }
function test() { $b = new B(); $b->bar(); }
print $t['class'] . $t['type'] . $t['function'] . "\n"; } } static function bar() { debug_print_backtrace(); $bt = debug_backtrace(); foreach ($bt as $t) { print $t['class'] . $t['type'] . $t['function'] . "\n"; } } } class B extends A { function __construct() { parent::__construct(); } function foo() { parent::foo(); } static function bar() { parent::bar(); } } $b = new B(); $b->foo(); B::bar();