This article mainly introduces the PHP in the static difference of this, has a certain reference value, now share to everyone, the need for friends can refer to
Reprinted from: https://blog.csdn.net/mrzhouxiaofei/article/details/78648079
Introduced
Recently in the course design of software engineering, encountered a problem, to grilled Laravel source code, and search, found that the self static understanding is not deep, only there is a problem, so record down to avoid making the same mistake again.
Body
This
This is a good understanding, which is to point to the current object, to access the current object's non-static variables and non-static methods, it is related to the object;
<?phpclass person {public $name; Public Function GetName () { echo $this->name; }} $p = new Person (), $p 2 = new Person (), $p->name = "Little Red"; $p 2->name = "Xiaoming"; $p->getname (); Xiao Hong $p2->getname (); Xiao ming
In the above example, new two objects and set the Name property of the object, GetName () uses this to access the Name property of the current object, so the value of name is output separately. So, this is pointing to the current object, not to another object or class.
Self
Self and this are different, it points to the class itself, does not point to any instantiated objects, and is generally used to access static variables and static methods in the class;
<?phpclass person {public static $name = "small red"; public static function GetName () { echo self:: $name; }} $p = new Person (), $p 2 = new Person (); $p:: GetName (); Xiao Hong $p2::getname (); Little Red $p:: $name = "Xiaoming"; $p:: GetName (); Xiao Ming $p2::getname (); Xiao ming
In the previous example, two objects were changed, and the Name property of one object was modified, and the value of the Name property of the other object was altered, so that self is pointing to the current class, regardless of the object, and all objects share a value.
Static
Static and self, are all points to the class, is generally used to access static variables and static methods in the class, but there are some different, specifically: Self write in which class, the actual call is this class, static is written in the parent class, and then through the subclass to use the static, This static is pointing to this subclass, officially called late static binding .
<?phpclass A {public function say () { echo "Hello"; } Public Function sayself () { self::say (); } Public Function saystatic () { static::say (); }} Class B extends A {public function say () { echo ' world '; }} $b = new B (); $b->say (); World$b->sayself (); Hello$b->saystatic (); World
As can be seen in the above example, self is written in Class A, the call point to a class, Static is also written in Class A, but the Class A subclass Class B object to call, but point to the Class B, when used, static to determine which class, which is the late static binding 。
Late static binding
Summarize
This points to the current object, which is used to access the non-static and non-static methods of the current object;
Self points to the class, which is generally used to access the static variables and static methods of the current class, which is determined before the run;
Static refers to a class, which is typically used to access static and static methods of the current class, but is not limited to static calls, and the runtime determines which class to point to.