PHP的静态成员和实例成员似乎没有什么不同。为什么 PHP 会这样做(没有警告)?
<?php
class A {
public function instanceFunc() {
echo "instance";
}
public static function staticFunc() {
echo "static";
}
}
A::instanceFunc(); // instance
$a = new A();
$a->staticFunc(); // static
这意味着在 PHP 中静态方法和实例方法没有任何区别。 Zend 甚至没有抱怨它(没有警告)。
在 Zend 引擎中。静态方法和实例方法都保存在zend_class_entry.function_table中。
为什么 PHP 会有这样的行为?这是一个错误还是功能?
<?php
class A {
public function instanceFunc() {
echo "instance";
}
public static function staticFunc() {
echo "static";
}
}
A::instanceFunc(); // instance
$a = new A();
$a->staticFunc(); // static
This means in PHP static method and instance method have no different at all.
Zend doesn't even complain about it(WITHOUT WARNING).
In Zend Engine. both static method and instance method all saved in zend_class_entry.function_table.
Why PHP behave like this? It's a bug or feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能与 Does static 重复PHP中的方法和非静态方法有什么区别?。
如果启用了 E_STRICT 警告,您将收到一条错误消息,否则,您可以静默调用非静态方法,就像它是静态方法一样。正如此处和另一个问题的答案中所提到的,如果无法访问
$this
或self
,那么现实世界的静态/非静态方法就不会走得太远,因为它被写入预计。参考文档:
http://www.php.net/manual/en/language。 oop5.static.php
这里有一些可以玩的东西:
http://codepad.org/Gbu9T6Zv
This is a potential duplicate of Does static method in PHP have any difference with non-static method?.
You will get an error message if you have E_STRICT warnings enabled, otherwise, you are able to silently call a nonstatic method as if it were static. As mentioned in answers here and at the other question, a real-world static/nonstatic method would not get far without having access to
$this
orself
as it was written to expect.Refer to the docs:
http://www.php.net/manual/en/language.oop5.static.php
Here's something to play with:
http://codepad.org/Gbu9T6Zv
如果您尝试运行下面的代码并查看回溯输出,您将看到 PHP 在静态上下文中调用时将 instanceFunc() 转换为静态方法。但是,在实例上下文中,它会将其视为实例调用。
如果将实例变量引入混合中(删除注释掉的行),那么从静态调用中调用 instanceFunc() 时会遇到致命错误。
这意味着 PHP 允许从静态上下文中调用本质上静态的所有方法(不使用实例变量),但一旦违反此约定,就会产生错误。因此,使用静态函数似乎是与其他面向对象语言保持一致的唯一良好实践。
对于 staticFunc() ,这两个调用都表明 PHP 将它们解释为静态调用,这是可以预料的。
示例输出(带有注释的代码运行):
If you try running the code below and look at the backtrace output you will see that PHP converts instanceFunc() to a static method when called in a static context. However, in an instance context it will treat it as a instance call.
If you introduce instance variables in to the mix (remove commented out lines) then a fatal error is encountered when called instanceFunc() from a static call.
This implies that PHP allows all methods that are static in nature (no instance variables are worked with) to be called from a static context, as soon as this contract is broken however an error will be produced. Therefore the use of static functions seems to be only good practice in keeping with other Object Orientated languages.
With regards to staticFunc() both calls show that PHP interprets these as static calls, which is to be expected.
Example output (code running with comments):
我不确定为什么它不会发出警告或至少不会发出错误。然而,静态和实例之间存在一些重大差异。静态方法不能使用非静态的类变量。通过添加非静态的公共/私有变量并尝试在 staticFunc 中回显它(这将引发错误),可以轻松测试该变量。
我认为主要目标是了解两者之间的差异以及如何正确使用它们。至于为什么 PHP 至少没有提出通知,我不知道,也许是由于其悠闲的性质。我对对此有更多了解的人可以给出的答复感兴趣,这只是一些额外的信息。
我确实在完整的错误报告打开的情况下对其进行了测试,果然它确实没有发出通知或警告。
更新
进行一些测试,似乎当您将未标记的静态函数作为静态函数调用时,您仍然无法使用私有/公共变量。对于正常功能来说,这很可能会出错。这可能就是为什么从未抛出错误或通知的原因。然而,它最好能抛出一些关于它的东西,因为以这种方式使用未标记的静态函数肯定是不好的。
I am not sure why it would not throw a warning or at least an error. However, there are some major differences in the static and instance. A static method cannot use class variables that are non-static. Which is easily tested by adding a public / private variable that is non-static and trying to echo it in the
staticFunc
that will throw an error.I think the main goal is to understand the differences between the two and how to properly use them. As far as why PHP does not at least raise a Notice, I do not know, perhaps it is due to its laid back nature. I am interested in the response someone who has more knowledge on it can give, this is kind of just a bit of extra information.
I did test it with full error reporting on, and sure enough it really does not throw a notice or warning.
UPDATE
Doing some testing, it seems as though when you call a non marked static function as a static function, you still cannot use the private/public variables. Which, for normal functions, will most likely error them out. Which could be why the error or notice was never thrown. However, it would be nice for it to throw something about it, as using a non-marked static function in such a way is surely not good to do.