PHP this-Self parent static contrast

Source: Internet
Author: User

This binds to the object that is currently instantiated

Such a long-appearing problem: If you use $this in a parent class to invoke a method or property of the current class, if the class is inherited and there is a property called in the corresponding subclass or the method is, the method or property in the parent class that is called by $this uses the method in the child class.

<?PHPclassbaseparent {Static $counter= 0; functionGetName () {return' Baseparent '; }                                       functionPrintname () {Echo $this->getname (). "\ n"; }               }                                       classBaseextendsbaseparent{Static $counter= 0; functionAddCounter () {returnSelf::$counter+=1; }                                       functionGetName () {return' Base '; }                                   }                                       $one=Newbase (); $one->printname ();

Output: Base


Parent points to the current object's parents class.

Self points to the current class itself, not to any objects that have been instantiated, and is typically used to point to static variables and static methods in a class: Because static methods and static variables cannot be instantiated into objects, they exist only in the current class

<?PHPclassbaseparent {Static $counter= 0;}classBaseextendsbaseparent{functionAddCounter () {returnSelf::$counter+=1; }}$one=Newbase ();Echo $one->addcounter (). "\ n";$two=Newbase ();Echo $two->addcounter (). "\ n";EchoBaseparent::$counter;

Output:1 2 2

Static late statically binding:
Static:: Is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time. It can also be called a "static binding",
Because it can be used (but not limited to) the invocation of a static method.

<?PHPclassbaseparent {Static $counter= 0; functionGetName () {return' Baseparent '; }                    functionPrintname () {Echo Static:: GetName (). "\ n"; }}                   classBaseextendsbaseparent{Static $counter= 0; functionAddCounter () {returnSelf::$counter+=1; }    functionGetName () {return' Base '; }               }$one=Newbase (); $one->printname ();

Output: Base

PHP this-Self parent static contrast

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.