Can static methods in php access non-static methods?

WBOY
Release: 2023-03-15 16:36:01
Original
2146 people have browsed it

Static methods in PHP can access non-static methods. By instantiating an object, you can call non-static methods in the object; although static methods can call non-static methods, they cannot call constructors.

Can static methods in php access non-static methods?

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

Can static methods in php access non-static methods

What happens if we call non-static methods? Do the test first.

<?php
class test{
    function test() {
        echo &#39;it works&#39;;
    }
}
test::test();
?>
Copy after login

Execute the following, and the error returned is as follows:

Fatal error: Non-static method test::test() cannot be called statically in /home/×××/test.php
on line 7 Call Stack: 0.0002 332548 1. {main}() /home/×××/test.php:0
Copy after login

At this time, you may think that calling non-static methods statically will not work, but in fact, it is too early to draw the conclusion. Because the test() method is special, it has the same name as the class and is a constructor method. We continue testing.

<?php
class test {
    function test() {
        echo &#39;it works&#39;;
    }
    function test2() {
        echo &#39;it works too&#39;;
    }
}
test::test2();
?>
Copy after login

Execution result:

it works too
Copy after login

This shows that statically calling non-static methods is feasible, but statically calling constructors is not. In order to verify this conclusion, I did the following test:

<?php
class test{
    static function test() {
        echo &#39;it works&#39;;
    }
}
test::test();
?>
Copy after login

The execution results are as follows:

Fatal error: Constructor test::test() cannot be static in /home/xxx/test.php on line 9
Copy after login

The constructor cannot be declared static, so the above inference is correct.

But this result is indeed very special, because maybe onlyPHP can statically call non-static methods. I did an experiment with Java. If the non-static method is statically called, the following error will be reported:

Cannot make a static reference to the non-static method showString() from the type HelloWorldApp
Copy after login

I have not tried other languages ​​one by one, but this is enough to illustrate the special features of PHP. I have not found any relevant explanations about why PHP has such a situation.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can static methods in php access non-static methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template