PHP in Self,this, parent detailed

Source: Internet
Author: User

1. Preface

Self,this,parent These three keywords in the use of the class is not uncommon, then the three keywords and what is the difference;
To understand from a literal meaning:

    • This is a pointer to the current class (closest rule, who is close to whom)
    • Self is a pointer to the current class (typically a static variable, used by a static method, referring to itself)
    • Parent is a pointer to the parents class (PHP is single-inheritance, and the parent class has only one)
2. This keyword

The test classes are as follows:

<?  1); class A {    public  $name;    function SetName (string  $name)     {        $this->name= $name;    }    function GetName ()    {        Var_dump ($This,name);}    }  class b extends a{        // here B inherits a but does nothing,    //B also has a method and properties }
2.1, so only to the Class A assignment, do not do any operation of class B
// This points to the current object New a (); $A->setname ('I am A; '); // The $this of this method points to class A, so the Class A property is assigned a value  New  B (); Echo $A'<br/>' ; Echo $B->getname ();

Output Result:

2.2, A, Class B simultaneous operation
New a (); $A->setname ('I am A; '); // The $this of this method points to class A, so the Class A property is assigned a value  New  B (); $B->setname ('I am B; '); // The $this of this method points to class B, so assigning a Class B property (Inherit a) echo $A'<br/>'; echo $B->getname ( );

Output Result:

2.3. Conclusion

The assignment of Class A does not always affect the value of Class B, so $this operation affects only the current class and does not affect the inheritance of the class;

3. Self keyword

First: It needs to be clear that self is pointing to the class itself, and that it has nothing to do with any object that has already been instantiated.

The test classes are as follows:

<?  1); class A {    static public $name;    function SetName (string  $name)     {        = $name;    }    function GetName ()    {        var_dump (self:: $name);}    } class b extends a{        // here B inherits a but does nothing,    //B also has a method and properties }
3.1, a class to operate
// the properties and methods of Class B are from Class A, and self points to class A New a (); $A->setname ('I am A; '); // The self of this method points to the class itself, so the assignment to class A will affect Class B  New  B (); Echo $A'<br/>' ; Echo $B->getname ();

Output Result:

3.2, Class A, Class B simultaneous operation
//the properties and methods of Class B are from Class A, and self points to class A$A =NewA (); $A->setname ('I am A;');//The self of this method points to class A, so the assignment to class A will affect Class B$B=NewB (); $B->setname ('I am B;');//The self of this method points to class A, so the assignment to class B affects the Class AEcho $A-getName (); Echo'<br/>'; Echo $B->getname ();

Output Result:

3.3. Class B Overrides all methods and properties of Class A
<?PHP Declare (strict_types=1);classA {Static  Public$name; function SetName (string$name) {self:: $name=$name;    } function GetName () {Var_dump (self:: $name); }}classB extends a{//Now Class B has its own methods and properties.    Static  Public$name; function SetName (string$name) {self:: $name=$name;    } function GetName () {Var_dump (self:: $name); }}//Class B Overrides all methods and properties of Class A, so self (a) points to Class A, self (b) to Class B,$A =NewA (); $A->setname ('I am A;');//The self of this method points to class A, so the assignment to Class A does not affect class B$B=NewB (); $B->setname ('I am B;');//The self of this method points to class B, so the assignment to Class B does not affect the Class AEcho $A-getName (); Echo'<br/>'; Echo $B->getname ();
   output result:          

3.4, Class B re-Class A partial methods and properties
<?PHP Declare (strict_types=1);classA {Static  Public$name; function SetName (string$name) {self:: $name=$name;    } function GetName () {Var_dump (self:: $name); }}classB extends a{//Class B now has some of its own methods and properties.    Static  Public$name; function SetName (string$name) {self:: $name=$name; }}//The GetName method inherits from Class A, so self in getName points to Class A$A =NewA (); $A->setname ('I am A;');//The self of this method points to class A, so the assignment to Class A does not affect class B$B=NewB (); $B->setname ('I am B;');//The self of this method points to class B, so the assignment to Class B does not affect the Class AEcho $A->getname ();//The self of this method points to class AEcho'<br/>'; Echo $B->getname ();//The self of this method points to class A

Result output:

3.5. Conclusion

From the above three sets of tests, it is not difficult to see, the first two examples, $name attribute only a class has, Class B although inherited (still not own), so self:: $name Regardless of the conditions point to Class A, resulting in the same results, and the third example, when given class B own properties and methods (re-class, Self has nothing to do with Class A, pointing to Class B, and the fourth example shows that the GetName method of Class B inherits Class A, so self points to class A.

Conclusion: Self points to the class itself (he does not matter who instantiates him, the value relates to the class he belongs to)

4. Parent keyword

First, it needs to be clear

    1. PHP is a single inheritance, and the parent class always has only one
    2. Parent can only call methods and cannot invoke properties
    3. The parent does not change this point
4.1. Call the parent class with this method
<?PHP Declare (strict_types=1);classA { Public$name; function SetName (string$name) {        $ This->name =$name; } function GetName () {var_dump ($ This-name); }}classB extends a{function getName () {var_dump ($ This-name);    } function Getnameparent () {parent::getname (); }} $A=NewA (); $A->setname ('I am A;'); $B=NewB (); Echo $A-getName (); Echo'<br/>'; Echo $B-getName (); Echo'<br/>'; Echo $B->getnameparent ();//call the Class A method, when the $this still point to Class B (the nearest (or so))

Output Result:

4.2, when the method of Class A is fixed output
<?PHP Declare (strict_types=1);classA { Public$name; function SetName (string$name) {        $ This->name =$name; } function GetName () {//var_dump ($this->name);Echo'Welcome to the Class A method'; }}classB extends a{function getName () {echo'Welcome to Class B method';    } function Getnameparent () {parent::getname (); }} $A=NewA (); $A->setname ('I am A;'); $B=NewB (); Echo $A-getName (); Echo'<br/>'; Echo $B-getName (); Echo'<br/>'; Echo $B->getnameparent ();//call the Class A method, when the $this still point to Class B (the nearest (or so))

Output Result:

4.3. Conclusion

The parent points to the parental class, but cannot change the direction of this in the parent class method, and can only access (public and protected), and the private error

5. Summary
    • This is a pointer to the current class (closest rule, who is close to whom)
    • Self is a pointer to the current class (typically a static variable, used by a static method, referring to itself)
    • Parent is a pointer to the parents class (PHP is single-inheritance, and the parent class has only one)

Basically I know so much, there must be a misunderstanding of the point, please master pointed out!

PHP in Self,this, parent detailed

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.