In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the difference between new static () and new self () in PHP, it has certain reference value, interested friends can refer to it, I hope you can learn a lot after reading this article, let the editor take you to know it.
When I found new static () when I was in coding, didn't I think the place to instantiate it should be new self ()? I checked to find out the difference between the two:
1) when there is subclass integration, the two behave differently.
2) the syntax of new static () is not supported in php 5.2or later.
In simple and popular terms, self is written in which class, which is actually called. The so-called late static binding, static represents the class used, which is the static that you write in the parent class.
Then the static is used directly / indirectly through the subclass, and the static refers to this subclass, so static is very similar to $this, but static can be used for static methods, properties, and so on.
The specific explanation is as follows:
Self-this is the class, the class in the code snippet.
Static-PHP 5.3 adds only the current class, which is a bit like $this, extracted from heap memory and accessed to the currently instantiated class, so static represents that class.
Let's take a look at the professional explanation of foreigners:
Self refers to the same class whose method the new operation takes place in.
Static in PHP 5.3 s late static bindings refers to whatever class in the hierarchy which you call the method on.
In the following example, B inherits both methods from A. Self is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see get_called_class ()).
The above code:
Class Person {public static function get_self () {return new self ();} public static function get_static () {return new static ();}} class WangBaoqiang extends Person {} echo get_class (WangBaoqiang::get_self ()); / / Personecho get_class (WangBaoqiang::get_static ()); / / WangBaoqiangecho get_class (Person::get_static ()); / / Person
But what if you want a subclass to use get_class and return the name of the current subclass ('wangbaoqiang') as well.
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.