Is PHP7 calling scope different from php5?

WBOY
Release: 2023-03-02 07:22:02
Original
837 people have browsed it

Today while studying __call() and __callStatic(), I read Brother Niao’s article PHP’s Calling Scope

But found that the results obtained by the following code are different in PHP5.5 and PHP7?

<code><?php
 class Foo {
     public function bar() {
         var_dump($this); //PHP5.5中打印的是A对象,PHP7是未定义,也就是NULL
     }
 }
 class A {
     public function test() {
         Foo::bar();
     }
 }
 $a  = new A();
 $a->test();
?></code>
Copy after login
Copy after login

I couldn’t find any instructions for adjusting this in PHP7. Thank you everyone for your advice

Reply content:

Today while studying __call() and __callStatic(), I read Brother Niao’s article PHP’s Calling Scope

But found that the results obtained by the following code are different in PHP5.5 and PHP7?

<code><?php
 class Foo {
     public function bar() {
         var_dump($this); //PHP5.5中打印的是A对象,PHP7是未定义,也就是NULL
     }
 }
 class A {
     public function test() {
         Foo::bar();
     }
 }
 $a  = new A();
 $a->test();
?></code>
Copy after login
Copy after login

I couldn’t find any instructions for adjusting this in PHP7. Thank you everyone for your advice

With this tweak, you can find it in PHP 7 which is not backwards compatible, and has been marked DEPRECATED in PHP 5.6

If you statically call a non-static method, you will be prompted that $this is not defined during the static call, and an error will be reported.

http://php.net/manual/en/migr...

http://m.runoob.com/php/php-d...

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!