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
但我觉得,PHP是默认把所有方法都认为是静态方法。静态方法,也可以通过实例整个类让这个方法变为一个对象的某个方法。所以默认全是静态方法,不会对类的功能上有任何影响。而这在C++中是不可以的,不声明为static方法,是不可以通过类名::方法名这样来调的。
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).
不过这种调用显然是有潜在问题的,因为这样调用没有$this指针.