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
According to null-values, the value of null in My...
Problem: When using JDBC to connect to the MySQL ...
Navigation and other things are often used in dai...
This article shares the specific code of JavaScri...
The default ssh remote port in Linux is 22. Somet...
1. Install the cross-system file transfer tool un...
This article shares the specific code of JavaScri...
We often need to control the hidden, transparent ...
This is an important (and wonderful) topic for Li...
Redux is a data state management plug-in. When us...
Installation environment: CentOS7 64-bit MINI ver...
MySQL official website: https://www.mysql.com/dow...
Table of contents In vue2 In vue3 Notes on setup ...
From: https://blog.csdn.net/qq_44761243/article/d...
External Access Randomly map ports Using the -P f...