在阅读一些框架的源码时发现了new static(),和new self(),甚是不解,后来查阅资料,才知道static采用了延迟绑定,能准确知道是父类还是子类的调用。这就是说static是个聪明的小孩,家里的亲戚的辈分他都能准确的叫出;而不是像他的兄弟self,只知道自己的亲爹妈。
例子如下:
<?php
classFather{
protectedstatic$name ="father";
publicstaticfunctionwhoami_self(){
echoself::$name."\n";
}
publicstaticfunctionwhoami_static(){
echostatic::$name."\n";
}
publicstaticfunctiongetInstance_self(){
returnnewself();
}
publicstaticfunctiongetInstance_static(){
returnnewstatic();
}
}
classFatherBrotherextendsFather{
protectedstatic$name ="uncle";
}
FatherBrother::whoami_self();//father
FatherBrother::whoami_static();//uncle
var_dump(FatherBrother::getInstance_self());//father
var_dump(var_dump(FatherBrother::getInstance_static()));//uncle