开发者

In PHP, is it a problem to call a static class function using the -> dereferencer

开发者 https://www.devze.com 2023-01-27 00:30 出处:网络
I am using PHP 5.2 I have the following code: class MyClass { public function __construct() {} public static function stuff() {

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 o开发者_Python百科f 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?


The docs explicitly say it's okay:

A property declared as static can not be accessed with an instantiated class object (though a static method can).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消