Application Analysis _php instance of PHP static call non-static method

Source: Internet
Author: User

Static Call Non-static Method!! This is unthinkable in the java,c#, and absolutely not. Such errors are pointed out incorrectly in these languages during the compile phase. But what about dynamic languages such as PHP? First of all this is the grammar is not wrong, php–l can not find the wrong. What if it runs?
First look at an example

Copy Code code as follows:

<?php
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 result of this code's final call? (PHP 5.3.10 running environment)

Looking at this result, there are several areas worth noting:

First is myclass::getname () this is a call to a static function
But looking at the MyClass getname () function, it's not a static function.

Second, the structure returned
First returns the PHP Strict standards Error. People familiar with PHP should know that Strict error is the code standardization warning for PHP, which is generally due to errors reported by PHP in order to maintain forward compatibility. So, the static invocation of non-static functions is allowed in a version prior to php5.3, only in the later version is not recommended for use!!

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

In the PHP4 version, the subclass needs to invoke the method of the parent class, but the subclass has a method with the same name, so the $this cannot be used, so PHP4 provides the method (Parentclassname::method ()). (Of course php5 added the keyword to the parent)

But the method provided by PhP4 actually allows static calls to a Non-static Method!! Background because of the need for forward compatibility, this feature has become a feature that cannot be removed (you must ensure that previous versions of the code can be run in the latter version of PHP).

Just add the strict error to prompt for this call.

Underlying implementation reason
Well, for the reason why this is happening, this article by Brother Bird has a note http://www.laruence.com/2012/06/14/2628.html

First of all need to subvert their own point of view, what is static call? Not to say: is static call, but see calling scope.

"The object that the $this pointer points to is the calling scope of this method being invoked."

I change a sentence to translate:

The static call is not calling scope, and the $this object in the $THIS->ABC () of the non-static call is calling scope.

Calling scope is passed when every sentence is called.

Understand the following code:

Copy Code code as follows:

<?php
Class A {
Public Function __construct () {
}
}
Class B extends A {
Public Function __construct () {
Parent::__construct ();
}
}


The Parent::_construct () here is the calling scope that subclasses convert calling scope into parent Class A. This is not a static call.

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

This time, because there is no use of the keyword such as parent, nor $this to the value, so calling scope does not change, is still $app. This means that at this point, all the $this pointers appear to be pointing to $app.

Well, the following things are well understood, Echo $this->name; Nature is invoking the Name property of calling scope.

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.