This article introduces the application analysis of PHP static call of non-static methods. For more information, see static call of non-static methods !! This is unimaginable in java and c #, and it is absolutely impossible. Such errors will be pointed out during the compilation phase in these languages. But what about dynamic languages like php? First, there is no syntax error. php-l cannot find the error. What if it is run?
Let's look at an example.
The Code is as follows:
Class 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 call result of this code? (Runtime environment PHP 5.3.10)
Here are several points worth noting:
MyClass: getName () is a static function call.
But taking a look at the getName () function of myClass, it is not a static function.
Second, the returned Structure
First, the PHP Strict Standards Error is returned. People familiar with php should know that Strict Error is the php code standardization warning, generally because php reports errors to keep forward compatibility. In this case, static calls to non-static functions are allowed in a version earlier than php5.3, but are not recommended in later versions !!
Now, if you comment out Strict Error in error_reporting, the returned result will become newClass !!
In the php4 version, the subclass needs to call the method of the parent class, but the subclass has a method of the same name, so $ this cannot be used, so php4 provides (parentClassName :: method. (Of course, the parent keyword is added to php5)
But this method provided by php4 actually allows static call of a non-static method !! Because the backend needs forward compatibility, this feature becomes a feature that cannot be deleted (you must ensure that the code of the previous version can be run in the php environment of the next version ).
Only Strict Error is added to prompt such calls.
Underlying implementation reasons
Well, for the reason why this situation occurs, this article of laruence has explained the http://www.laruence.com/2012/06/14/2628.html
First, we need to overturn our own point of view. What is static call? It does not mean that there are: static calls, but calling scope.
"$ This pointer points to the calling scope of the method called ."
For another Sentence Translation:
There is no calling scope for static calls. The $ this in $ this-> abc () of a non-static call points to the calling scope object.
Calling scope is passed when each sentence is called.
Understand the following code:
The Code is as follows:
Class {
Public function _ construct (){
}
}
Class B extends {
Public function _ construct (){
Parent: :__ construct ();
}
}
Here, parent: _ construct () is A subclass that converts calling scope to the calling scope of the parent Class. This is not a static call.
Back to the top example
MyClass: getName ();
At this time, because no keyword such as parent is used and $ this is not assigned a new value, the calling scope is not changed and is still $ app. That is to say, all the $ this pointers that appear at this time point to $ app.
Well, the following things are easy to understand. echo $ this-> name; is naturally calling the name attribute of calling scope.