<?phpclass myclass{ Private $name = "MyClass"; Public Function Echoname () { echo $this->name; } Public Function GetName () { $this->echoname () }}} class newclass{ private $name = "Newclass"; Public Function Echoname () { echo $this->name; } Public Function test () { myclass::getname (); echo "\ n"; }} $app = new Newclass (); $app->test ();
What is the final invocation result of this code? (Running environment PHP 5.3.10)
Returns PHP Strict standards Error. People familiar with PHP should know that Strict error is a code normalization warning for PHP, generally due to PHP's error in keeping forward compatibility. That said, static calls to non-static functions are allowed in a version prior to php5.3, but only in later versions are not recommended!!
If you now comment out strict error in error_reporting, the return result becomes newclass!!
What is this for? According to our understanding, even if the call is MyClass's GetName () method, the return should also be "MyClass", Why is "Newclass" it?
What is a static call? It's not that: it's a static call, but a calling scope.
In PHP, when a method is called, the object that the $this Pointer to is the calling scope at which the method is called.
<?php Foo::bar (); ? >
When the bar method is called, it is in a context that does not have a calling scope domain, so this is a static call.
And for the following example:
<?php class A {public function test () { foo::bar (); } } $a = new A (); $a->test ();
When calling the bar method, in the context of a $ A $ object, that is, the calling scope at this time is a $ A object, so this is not actually a static call.
To verify this conclusion, consider one of the following practical examples:
<?php class Foo {public function bar () { var_dump ($this); } } Class A {public function test () { foo::bar (); } } $a = new A (); $a->test (); ? > Output: object (A) #1 (0) {}
In the call to bar, this seemingly "static" invocation of the call, $this pointer is assigned value, point to a $ A object, then this is static call it?
The problem arises because a non-static method of a class is invoked in the context of a calling scope with a "static form".
<?php Class A {public function __construct () { } } class B extends A {public function __cons Truct () { parent::__construct (); } }
When we call the constructor of the parent class, we deliberately pass the current scope to the constructor of the parent class as calling scope (here Parent::_construct () is the subclass calling Scope is converted to the calling scope of parent Class A. This is not a static call)
A static call is not calling scope, and a non-static call to this object is calling scope.
Calling scope is passed when each sentence is called.
This time because there is no use of a keyword such as the parent, nor the this re-assignment, calling scope is still the app, that is, all the present this pointer to the app.
Echo $this->name; It is natural to invoke the name attribute of calling scope.
Static calls to non-static methods