PHP static call non-static method application Analysis _php instance

Source: Internet
Author: User
Static call non-static Method!! This is unthinkable in the java,c#, and absolutely not. Such errors are indicated incorrectly in these languages during the compilation phase. But what about dynamic languages like PHP? First of all, this is a grammar without errors, php–l can not find a mistake. What if you run it?
let's look at an example.
Copy CodeThe 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 invocation result of this code? (Running environment PHP 5.3.10)

To see the results, there are several places to note:

The first is myclass::getname () this is a call to a static function
But look at MyClass's GetName () function, but it's not a static function.

The structure returned next
First 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!!

Well, if you now comment out strict error in error_reporting, the return result becomes newclass!!

In the PHP4 version, the subclass needs to call the parent class's method, but the subclass has a method with the same name and therefore cannot use $this, so PhP4 provides a method (Parentclassname::method ()). (Of course php5 added the keyword of the parent)

But the method provided by PHP4 is actually allowing static calls to a non-static method!! Background because of the need for forward compatibility, this feature becomes a feature that cannot be removed (it is important to ensure that the previous version of the code can be run in the latter version of the PHP environment).

Just add strict error to prompt for such a call.

Underlying implementation reasons
Well, for the reasons why this situation occurs, bird Brother's article has a description http://www.laruence.com/2012/06/14/2628.html

The first thing to do is to subvert your point of view, what is static invocation? It's not that: it's a static call, but a calling scope.

The object pointed to by the $this pointer is the calling scope at which the method is called.

I change a sentence to translate:

Static calls are not calling scope, and non-static calls to $THIS->ABC () $this point to an object that is calling scope.

Calling scope is passed when each sentence is called.

Understand the following code:
Copy the Code code as follows:
Class A {
Public Function __construct () {
}
}
Class B extends A {
Public Function __construct () {
Parent::__construct ();
}
}


The Parent::_construct () here is a subclass of calling scope that translates calling scope to parent Class A. This is not a static call.

Back to the top example
Myclass::getname ();

At this point, there was no use of a keyword such as parent, and no $this was re-assigned, so calling scope did not change, and still $app. That is, all the $this pointers that appear at this time are pointing to $app.

Well, the following is a good understanding, echo $this->name; It is natural to invoke the name attribute of calling scope.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.