Calling non-static method with double-colon(::)(使用双冒号(::)调用非静态方法)
问题描述
为什么我不能使用具有方法 static(class::method) 语法的非静态方法?这是某种配置问题吗?
Why can't I use a method non-static with the syntax of the methods static(class::method) ? Is it some kind of configuration issue?
class Teste {
public function fun1() {
echo 'fun1';
}
public static function fun2() {
echo "static fun2" ;
}
}
Teste::fun1(); // why?
Teste::fun2(); //ok - is a static method
推荐答案
PHP 在静态与非静态方法方面非常松散.我在这里没有看到的一件事是,如果您从 C
, 类的非静态方法中静态调用非静态方法
将引用您的 ns
ns
中的 $thisC
实例.
PHP is very loose with static vs. non-static methods. One thing I don't see noted here is that if you call a non-static method, ns
statically from within a non-static method of class C
, $this
inside ns
will refer to your instance of C
.
class A
{
public function test()
{
echo $this->name;
}
}
class C
{
public function q()
{
$this->name = 'hello';
A::test();
}
}
$c = new C;
$c->q();// prints hello
如果您有严格的错误报告,这实际上是某种错误,否则不是.
This is actually an error of some kind if you have strict error reporting on, but not otherwise.
这篇关于使用双冒号(::)调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用双冒号(::)调用非静态方法


- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01