In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what's the difference between $this,self,static in PHP". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what's the difference between $this,self,static in PHP"?
Study time
To put it simply
$this points to the current object instance, and self points to the current class.
In other words:
$this- > member calls non-static properties / methods, and self::number calls static properties / methods.
Take a chestnut, which is more intuitive, saying that the concept is always so boring.
The above example explicitly uses static and dynamic properties, which are called within the constructor. Executed when instantiated.
It would be wrong if you use it the other way around, such as using:
Self::$non_static_member. ''. $this- > static_member
Self calls non-static attributes, while $this invokes static attributes, which is the wrong use.
The following demonstrates the polymorphism of the class using an overloaded function method with a $this object property / method call:
After the above file is executed, the value Y::foo () is returned. As an instantiated Y object, $this accesses its dynamic method foo () directly. The foo () method of the inherited X class is overridden and does not execute.
Now write it in a different way.
This time we use the self::foo () call to the bar method of class X. So obviously, self is class X itself, so it's natural to call X's foo method. The above program outputs X::foo ().
Extension: try not to use self::, but use static::
Self is simple and easy to use, but the scope is annoying. Because its scope is defined, not executed. Take an example like this:
If Person::status () is called, it returns' Person is alive'. Now create a new class and inherit it:
When you execute Deceased::status (), what do you expect? it must be the value returned by the getStatus () of the Decased class, right? However, the result returns the value of Person::status (). This is because when the status method is called, it uses self::getStatus () and accesses the getStatus () method of the Person class. This is determined by the self scope.
How can I return the expected value? Replace self with static.
The same rule: $this refers to an instance of the current class, and static refers to the current class itself.
We learn usage from good code. Here is a static method within the Laravel ValidationData class.
Thank you for your reading, the above is "what's the difference between $this,self,static in PHP", after the study of this article, I believe you have a deeper understanding of what is the difference between $this,self,static in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.