Recently in See Laravel Source, the beginning of the static keyword usage let me read the confused,
Static variables should not be used to define methods and properties?
In the method
Static::setinstance ($this);
What is it?
Check the manual to understand that this is another use of the static keyword, "Late static binding", also known as "static binding"
http://php.net/manual/zh/language.oop5.late-static-bindings.php
The official introduction:
This feature is named "Late static binding" from the perspective of the language itself. Late binding means thatstatic:: It 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.
The official examples are a good illustration of the meaning of late static bindings
Self:: the limit
Use self:: or __class__ a static reference to the current class, depending on the class in which the current method is defined:
Example #1 Self:: Usage
<?phpclass A {public static function who () {echo __class__; } public static function test () {self::who (); }}class B extends A {public static function who () {echo __class__; }}b::test ();? >
Output: A
Use of late static bindings
Late static binding This is to bypass the restriction by introducing a new keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to invoke test () in the above example to refer to the class B instead of a. The final decision is not to introduce a new keyword, but instead to use the static keyword that has been reserved.
Example #2 static:: Simple usage
<?phpclass A {public static function who () {echo __class__; public static function test () {static::who ();//Late static binding starts here}}class B extends A {public static functi On the WHO () {echo __class__; }}b::test ();? >
Output: B
Here static is equivalent to the Self,parent keyword, used to invoke the method, but unlike self, static calls to the class is not necessarily static properties and static methods, which is closer to the use of the keyword parent.
But, in the following example, we also see a usage
<?phpclass model{public static function find () {echo static:: $name; }}class product extends model{protected static $name = ' product ';} Product::find ();? >
The $name attribute is not defined in the parent class and must be
Static $name
However, the general program should not have this kind of parent without defining attributes and directly use the case.
-----------------------------------------------------------------------------------------------------------
In addition, when you test an example, you think of such a situation
Class A {public static function who () {echo __class__; } Public Function Test () {static::who (); }}class B extends A {public static function who () {echo __class__; }} (New B ())->test ();
A non-static method can call a static method, or vice versa.
This static binding method is used extensively in the laravel design pattern, especially the core function facades.
PHP Object-oriented programming there are a lot of things to know ...
Late static binding of PHP object-oriented Programming note static