PHP静态函数在动态环境中调用

标签 php class dynamic static-methods

从什么时候 PHP 允许像动态函数一样调用静态函数了?

我使用的是 php 5.3.2

class weird{

    public static function iamstatic($calledFrom){
            echo "I am  a static function called with  a $calledFrom operator\n";
    }

    public function test(){
            self::iamstatic("static");
            $this->iamstatic("dynamic");
    }

 }

$c = new weird();
$c->test();

weird::iamstatic("Static outside class");
$c->iamstatic("Dynamic outside class");

输出:

I am  a static function called with  a static operator
I am  a static function called with  a dynamic operator
I am  a static function called with  a Static outside class operator
I am  a static function called with  a Dynamic outside class operator

最佳答案

php5.0及以上版本总是可以的。

http://3v4l.org/14PYp#v500

此外,文档中提到了它 ( static )

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

这不是一个错误 ( static methods assigned to instances )

关于PHP静态函数在动态环境中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795599/

相关文章:

C++ - 只有静态成员的类是不好的做法/反模式吗?

java - 反编译java类文件

java - 当父类是引用类型并通过方法传递时,如何从子类访问属性?

PHP 获取数组变量提交后的值

php - 将对象传递给方法,将该对象分配给另一个对象,传入的对象仍然是我传入的同一对象

php - 全新安装的 laravel 项目无法访问 postgresql 数据库,但可以执行 php migrate

c++ - 不使用 vector 的动态字符串数组

c# - C#\Dynamic\如果动态var为null(对象类型)或零(值类型),我是否可以 checkin 一条语句

javascript - php中的相对url

php - 如何检查用户是否存在于MySQL数据库中?