当我们用静态方式调用非静态函数时会发生什么?
喜欢这个:
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来自不兼容的上下文