1:
<?php
class classname{
function classname(){ }
function nostaicfun (){
echo "nosta";
classname::staticfun();
}
function staticfun(){
echo "sta";
}
}
$class = new classname;
$class->nostaicfun();
?>
2:
<?php
class classname{
function classname(){

}
function nostaicfun (){
echo "nosta";
}
function staticfun(){
echo "sta";
}
}
classname::staticfun();
?>测试了1和2都可以!!
但staticfun方法没有用static来修饰,为什么却都可以classname::staticfun()调用????
php5.2.6

解决方案 »

  1.   

    没看过php这块的源代码,所以不敢肯定。
    但我觉得,PHP是默认把所有方法都认为是静态方法。静态方法,也可以通过实例整个类让这个方法变为一个对象的某个方法。所以默认全是静态方法,不会对类的功能上有任何影响。而这在C++中是不可以的,不声明为static方法,是不可以通过类名::方法名这样来调的。
      

  2.   

    这应该归功于PHP的灵活性吧。"运行时绑定"就是比编译型语言的"编译时绑定"灵活的多。
      

  3.   

    有意思的一个发现 在手册上看static发现如下一段 但奇怪的是楼主第一种没有报错class Test { 
       
       static function static_method() { 
           echo "Here's your static method: Foo!<br />\n"; 
       } 
       function static_method_caller() { 
           echo "static_method_caller says:  ";$this->static_method();    
       } 
       function non_static() { 
           echo "I am not a static method<br />\n"; 
       } } $t = new Test(); 
    $t->static_method(); 
    $t->static_method_caller(); 
    Test::non_static(); 
    [Editor's Note: This is done for back compatability. Depending on your error level, An E_STRICT error will be thrown.] PHP 5.0.1 doesn't seem to mind if you call a static method in a non-static context, though it might not be the best of style to do so. On the other hand, PHP complains if you try to try to call a non-static method in a static context (if your error reporting is cranked up to E_STRICT). 
      

  4.   

    这个注释说了是为了兼容...而且要E_STRICT错误打开才能看见报错.
    不过这种调用显然是有潜在问题的,因为这样调用没有$this指针.