In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use self in laravel". In daily operation, I believe many people have doubts about how to use self in laravel. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use self in laravel". Next, please follow the editor to study!
In laravel, the self keyword is used to replace the class name, it can refer to the static member variables and static functions of the current class, it can also be used to suppress polymorphic behavior, it can refer to the function of the current class instead of the implementation overridden in the subclass, and self always points to the current class and class instances.
This article operating environment: Windows10 system, Laravel6 version, Dell G3 computer.
What is the use of self in laravel
Non-member functions cannot be called with this within static member functions, but static member functions / variables / constants can be called with self; other member functions can call static member functions and non-static member functions with self. As the discussion went on, it was found that self was not that simple. In view of this, this paper first compares and distinguishes several keywords, and then summarizes the usage of self.
Differences from parent, static and this
To get a thorough understanding of self, you should distinguish it from parent, static, and this. The following are compared.
Parent
The distinction between self and parent is easier: parent refers to methods (or variables) that are hidden from the parent / base class, and self refers to its own methods (or variables). For example, the parent constructor is called in the constructor:
Class Base {public function _ _ construct () {echo "Base contructor!", PHP_EOL;}} class Child {public function _ construct () {parent::__construct (); echo "Child contructor!", PHP_EOL;}} new Child;// output: / / Base contructorhammer / Child contructorstatic
The general use of static is to modify functions or variables to become class functions and class variables, or to modify variables within functions to extend their life cycle to the entire application life cycle. But its association with self is a new use introduced since PHP 5.3: static deferred binding.
With the static deferred binding capabilities of static, you can dynamically determine which classes to belong to at run time. For example:
Class Base {public function _ construct () {echo "Base constructor!", PHP_EOL;} public static function getSelf () {return new self ();} public static function getInstance () {return new static ();} public function selfFoo () {return self::foo ();} public function staticFoo () {return static::foo () } public function thisFoo () {return $this- > foo ();} public function foo () {echo "Base Foo!", PHP_EOL;}} class Child extends Base {public function _ construct () {echo "Child constructor!", PHP_EOL;} public function foo () {echo "Child Foo!", PHP_EOL;}} $base = Child::getSelf () $child = Child::getInstance (); $child- > selfFoo (); $child- > staticFoo (); $child- > thisFoo ()
The output of the program is as follows:
Base constructor!Child constructor!Base Foo!Child Foo!Child Foo!
In terms of function references, the difference between self and static is: for static member functions, self points to the current class of the code, and static points to the calling class; for non-static member functions, self suppresses polymorphism, points to the member functions of the current class, and static is equivalent to this, and dynamically points to the function of the calling class.
It's interesting to combine the three keywords parent, self and static, pointing to the parent class, the current class, and the subclass, respectively, with a taste of "past, present, and future".
This
Self and this are the most discussed and most misused combinations. The main differences between the two are as follows:
This cannot be used in static member functions, self can
For access to static member functions / variables, self is recommended, not in the form of $this:: or $this- >
For access to non-static member variables, you cannot use self, only this
This is to be used when the object is already instantiated. Self does not have this restriction.
Used within non-static member functions, self suppresses polymorphic behavior and references the function of the current class, while the this reference calls the class's rewrite (override) function, if any.
The purpose of self
After reading the difference with the above three keywords, is the use of self on call? In a word, self always points to "current class (and class instance)". In detail, it is as follows:
Replace the class name and refer to the static member variables and static functions of the current class
Suppresses polymorphic behavior and references the function of the current class instead of the implementation of overrides in the subclass
Slot point
Among these keywords, only this needs to add a $sign and must be added. Obsessive-compulsive disorder indicates that it is very uncomfortable.
Non-static member functions cannot be called through $this- > in static member functions, but can be called through self:: and run smoothly without using $this- > in the calling function. This behavior seems to behave differently in different versions of PHP, ok in the current 7.3
Output self in static and non-static functions, and guess what the result is? They are all string (4) "self", the output of mystery.
There are syntax errors in return $this instanceof static::class;, but the following two ways of writing are normal:
$class = static::class
Return $this instanceof $class
/ / or like this:
Return $this instanceof static
At this point, the study on "how to use self in laravel" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.