Use self:: or __CLASS__ to get a static reference to the current class, depending on the class in which the method is defined: Using static:: is no longer resolved to the class where the current method is defined, but is actually calculated at runtime. It can also be called "static binding" because it can be used for (but is not limited to) calling static methods. Static binding is a feature added in PHP 5.3.0 that is used to reference statically called classes in the inheritance scope. The difference between static and self in PHP classes. We often see self and static in the definition of PHP classes. When running, we often find that the results are no different, but there is definitely a difference. If there is no difference, why are there two? 1. The difference between the twoStatic uses delayed binding Therefore, static can accurately know whether it is a call from the parent class or the child class. For example, this shows that Static is a smart kid. He can accurately recognize all his relatives. Self is a little bit stupid, he only knows your own parents 2. Code looks differentIt can be seen from the following code self refers to the class that defines him, that is, he only knows his own parents static refers to the class that calls him, that is, he can recognize his relatives <?php class parents { protected static $name = 'I am your parent'; public static function who_self() { return self::$name; } public static function who_static() { return static::$name; } public static function get_self() { return new self(); } public static function get_static() { return new static(); } } class kinsfolk extends parents { protected static $name = 'I am your relative'; } var_dump(kinsfolk::who_self()); //I am your parentvar_dump(kinsfolk::who_static()); //I am your relativevar_dump(kinsfolk::get_self()); //object(parents)#1 (0) {} var_dump(kinsfolk::get_static()); //object(kinsfolk)#1 (0) {} In simple terms, Self is the class in which it is written, and this class is actually called. static represents the class being used, that is, the static you wrote in the parent class, and then it is overwritten by the subclass, and the method or attribute of the subclass is used SummarizeThis is the end of this article about the difference between static and self in PHP class. For more information about the difference between static and self in PHP class, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Modify the boot time of grub in ubuntu
>>: Explanation of MySQL index types Normal, Unique and Full Text
0x0 Test Environment The headquarters production ...
1. Data Deduplication In daily work, there may be...
This article introduces 5 ways to solve the 1px b...
Installation Environment Description •System vers...
This article shares the installation method of My...
1. Create a centos7.6 system and optimize the sys...
Table of contents 1. What is scrapy_splash? 2. Th...
Table of contents CentOS rpm installation and con...
1. Add the ul tag in the body first <!-- Unord...
We often want to find a file in Linux, but we don...
1. Go to the location where you want to store the...
Preface In backend development, in order to preve...
Table of contents 1. What is JSON 1.1 Array liter...
1. To optimize the query, try to avoid full table...
Table of contents 1. Download steps 2. Configure ...