PHP 5.3 后期静态绑定 理解及测试
<?php
class A {
private function foo() {
echo "success!\n";
}
public function test() {
// $this->foo();
self::foo();
static::foo();
}
}
class B extends A {
/* foo() will be copied to B, hence its scope will still be A and
* the call be successful */
}
class C extends A {
private function foo() {
echo 11111111111;
/* original method is replaced; the scope of the new one is C */
}
}
$b = new B();
$b->test();
$c = new C();
$c->test(); //fails
即$c中的 调用 foo() 为class c的方法,故为后期绑定
=================
<?php
class A {
public/protected function foo() { //只有private 时才可以正常
echo "success!\n";
}
public function test() {
$this->foo();
// static::foo();
}
}
class C extends A {
private static function foo() {
echo 11111111111;
} }
$c = new C(); $c->test(); //fails
即 父类有的方法子类不能用static重写。 //只有private 时才可以正常
+=================
子类重写父类方法 如果定义为static 则父类必须也为static
以上是个人测试的结果。如有不当之处 欢迎交流 ;QQ 2089764