在PHP中,使用->调用静态类函数有问题吗?解引用器
我正在使用 PHP 5.2
我有以下代码:
class MyClass {
public function __construct() {}
public static function stuff() {
echo 'This is static! <br />';
}
}
$myClass = new MyClass();
MyClass::stuff(); // Reference by class.
$myClass->stuff(); // Reference by instance of class.
输出在两种情况下都有效 这里是输出:
这是静态的!
这是静态的!
使用第二种引用方式与第一种方式相比?
由于我不允许拥有与上面的静态函数具有相同签名的非静态函数,因此这不会成为问题。我希望该函数是静态的,因为使用静态函数时也会提高速度。
我是否遗漏了任何内容,或者这是关于 -> 的语义的唯一问题取消引用语法并不表明这是一个静态函数?
I am using PHP 5.2
I have the following code:
class MyClass {
public function __construct() {}
public static function stuff() {
echo 'This is static! <br />';
}
}
$myClass = new MyClass();
MyClass::stuff(); // Reference by class.
$myClass->stuff(); // Reference by instance of class.
The output works in both cases here is the output:
This is static!
This is static!
Is there a problem using the 2nd way of referencing versus the 1st?
Since I am not allowed to have a non-static function with the same signature as the static one above that won't be an issue. I want the function to be static because there is also a speed boost when using static functions.
Am I missing anything or is the only issue here regarding the semantics of how the -> dereference syntax does not indicate this is a static function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
docs 明确表示没问题:
不过,使用
::
更清晰。我还质疑静态方法明显更快的想法,特别是在不使用实例字段时。在开始更改应用程序的语义以提高性能之前,您应该进行分析。The docs explicitly say it's okay:
However, it's clearer to use
::
. I also question the idea that the static method is significantly faster, particularly when no instance fields are used. You should do profiling before you start altering the semantics of your application for performance.