In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use self in php, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use self in php. Let's take a look at it.
In PHP, self means "oneself, itself"; the self keyword is used to point to the class itself and to the class instance, and the self keyword is not to point to any object that has been instantiated, but to point to static variables in the class.
This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.
What does self mean in php
The use of self in php:
Self points to the class itself, that is, the PHP self keyword does not point to any objects that have been instantiated, and self is generally used to point to static variables in the class.
1. Self can access static properties and static methods in this class, and can access static properties and static methods in the parent class.
2. When using self, you don't have to instantiate it.
< ?phpclass Counter{undefined//定义属性,包括一个静态变量private static $firstCount = 0;private $lastCount;//构造函数function __construct(){undefined$this->LastCount = selft::$firstCount;// uses the PHP self keyword to call static variables, and self calls must use: (field operation symbol)} / / print the maximum value function printLastCount () {undefinedprint ($this- > lastCount);}} / / instantiate the object $countObject = new Counter (); $countObject- > printLastCount (); / / output 1? >
We only need to pay attention to two places here, line 6 and line 12.
We defined a static variable $firstCount on the second line, with an initial value of 0, so we called this value on line 12, using self to call, and using "::" to connect.
Is what we call the domain operator, so what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it just has to do with the class.
So I call the class itself, so we can't use this to reference, we can use self to reference.
Because self points to the class itself, it has nothing to do with any object instance. In other words, if there are static members in our class, we must also use self to call.
This is the end of the article on "how to use self in php". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use self in php". If you want to learn more, you are welcome to follow the industry information channel.
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.