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