Why can PHP directly call non-static methods of a class through ::?
滿天的星座
滿天的星座 2017-06-10 09:47:28
0
7
558

code show as below:

<?php

class Demo
{
    public function testing()
    {
        echo "testing\n";
    }
}

Demo::testing();

php7.0 execution output:

$ php demo.php 
testing

php5.6 execution output

$ php demo.php
PHP Strict Standards:  Non-static method Demo::testing() should not be called statically in /home/runner/Code/funny/demo.php on line 11

testing

Is there such an operation?? What is the principle??

滿天的星座
滿天的星座

reply all(7)
大家讲道理

http://www.laruence.com/2012/...

Hahaha, thank you all for your answers. For the specific reasons, please read Brother Bird’s article above

小葫芦

If a class below version 5.6 directly calls methods and attributes without instantiation, the methods and attributes must be static methods, that is, if the Demo class directly calls the testing method, the test must be public static function testing() { }. Otherwise, an error will be reported.
I don’t know if there is no need to declare anything above 7.0

扔个三星炸死你

I saw it, but php-cli can execute it, but php-fpm still can’t

Deprecated: Non-static method Demo::testing() should not be called statically in test.php on line 11

testing

某草草

Although this can be used, it is not recommended.

習慣沉默

php7.0 can be written like this, but php5.6 definitely cannot. It can also be written as self::testing

大家讲道理

__callStatic()

学习ing

It can be considered that the implementation of PHP is not rigorous.

If $this is not used in the non-static method, you can use:: to call it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template