PHP调用非静态函数与静态方式


PHP Call non static function with static way

当我们用静态方式调用非静态函数时会发生什么?
喜欢这个:

class test
{
    public function hello()
    {
        return "say hello";
    }
}
class foo
{
    public function __construct()
    {
        echo test::hello();
    }
}
$foo = new foo();

你会得到Strict standards: Non-static method test::hello() should not be called statically

而你的类声明不好,正确的方式是这样的

class test
{
    public function hello()
    {
        return "say hello";
    }
}
class foo
{
    public function __construct()
    {
        echo test::hello();
    }
}
$foo = new foo();

Laravel使用Facade来给人一种你正在使用static方法的错觉,你也可以使用Facade来调用方法,如test::hello();

这是一本关于Facade的好读物

快速回答:您会收到严格的警告。

更多详细信息:如果在调用的函数中使用变量$this,则它指的是调用函数的对象,而不是包含函数本身的类。

严格标准:非静态方法不宜叫你好 静态地,假设$this来自不兼容的上下文