In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the difference between self and static in PHP". In daily operation, I believe that many people have doubts about the difference between self and static in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "the difference between self and static in PHP". Next, please follow the editor to study!
On self and static in PHP
The delivery of self, static, and parent has been introduced in previous articles. Today, through a small experiment, let's look at the problem of self and static manipulating variables.
Class A
{
Public static $name = "Ichimm A!"
Public function selfName ()
{
Echo self::$name
}
Public function staticName ()
{
Echo static::$name
}
}
Class B extends A {
Public static $name = "iTunm B!"
}
B = new B ()
$b-> selfName (); / / Ichimm A!
$b-> staticName (); / / Isimm B!
Class C extends A {
Public static $name = "Ichimm C!"
Public function selfName ()
{
Echo self::$name
}
}
C = new C ()
$c-> selfName (); / / Iambim C!
$c-> staticName (); / / Iambim C!
Through this simple example, we can see two points:
The class in which self is written, it always points to the current class static, which object calls it, and it points to the caller.
We can see from the code that class B does not override the selfName () method, so when class B calls selfName (), it calls the selfName () method of parent class A, and self points to class An in this method. Class C overrides the selfName () method of the parent class, and when calling class C's selfName (), the self in it points to the class itself.
Static is relatively simple, in the example, the staticName () method of class An is called by class B and class C, and the output is in line with our expectations. Class B objects point to class B, and class C objects point to class C.
At this point, the study of "the difference between self and static in PHP" 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.