In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand static variables in PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand static variables in PHP".
The static keyword declares that a property or method is related to a class, not to a particular instance of the class, so such properties or methods are also called "class properties" or "class methods".
If access control permissions allow, you can use the class name with two colons instead of creating the class object.
The static keyword can be used to modify variables and methods.
Without instantiation, you can directly access the properties of static and the methods of static in the class.
The properties and methods of static can only access the properties and methods of static, and classes cannot access non-static properties and methods. Because when static properties and methods are created, there may not be any instances of this class that can be called.
The property of static, which has only one copy in memory, is shared by all instances.
Use the self:: keyword to access static members of the current class.
Common properties of static properties
All instances of a class share static properties in the class.
That is, even if there are multiple instances in memory, there is only one static property.
The example below sets a counter $count property and sets the private and static decorations. In this way, the $count property is not directly accessible to the outside world. As a result of running the program, we also see multiple instances using the same static $count property.
The copy code is as follows:
The result of running the program:
one
two
Now here have 3 user
Now here have 2 user jb51.net
Static properties are called directly
Static properties can be used directly without instantiation, and can be used directly before the class is created.
The method used is the class name:: static property name.
The copy code is as follows:
The result of running the program:
one
two
three
four
The area with a radius of 3 is
28.26
The area with a radius of 3 is
28.2743338823
Class is not created, static properties can be used directly. When is the static property created in memory? There is no relevant information in PHP. Citing the concepts in Java to explain should also be universal.
Static properties and methods are created when the class is called. A class is called when the class is created or any static member of the class is called.
Static method
Static methods can be used directly without the need for the class to be instantiated.
The method used is the class name:: static method name.
Let's go on to write this Math class for mathematical calculations. We design a method to calculate the maximum value. Since it is a mathematical operation, there is no need to instantiate this class, and it would be much more convenient if this method could be taken over.
We are only designing this class to demonstrate the static method. The max () function is provided in PHP to compare values.
The copy code is as follows:
The result of running the program:
Displays that the maximum values in $an and $b are
ninety-nine
Displays that the maximum values in $an and $b are
one hundred
How static methods call static methods
In the first example, when a static method calls another static method, it uses the class name directly.
The copy code is as follows:
The result of running the program:
one
two
The maximum value in display 99 77 88 is
ninety-nine
You can also use self:: to call other static methods in the current class. (recommended)
The copy code is as follows:
The result of running the program:
one
two
The maximum value in display 99 77 88 is
ninety-nine
Static method call static property
Use the class name:: static property name to invoke static properties in this class.
The copy code is as follows:
The result of running the program:
one
The area of a circle with radius 3 is 28.26.
Use self:: to call the static properties of this class. (recommended)
The copy code is as follows:
The result of running the program:
one
The area of a circle with radius 3 is 28.26.
Static methods cannot call non-static properties
Static methods cannot call non-static properties. You cannot use self:: to call non-static properties.
The copy code is as follows:
The result of running the program:
one
Fatal error: Undefined class constant 'pi' in E:PHPProjectstest.php on line 7
Nor can you use $this to get the value of a non-static attribute.
The copy code is as follows:
The result of running the program:
one
Fatal error: Using $this when not in object context in E:PHPProjectstest.php on line 7
Static method calls non-static method
In PHP5, non-static methods cannot be called using the $this identity in static methods.
The copy code is as follows:
The result of running the program:
The maximum value in display 99 77 188 is
Fatal error: Using $this when not in object context in E:test.php on line 10
When a non-static method in a class is called by self::, the system automatically converts the method to a static method.
The following code is executed and has a result. Because the Max method is converted to a static method by the system.
The copy code is as follows:
The result of running the program:
one
two
The maximum value in display 99 77 188 is
one hundred and eighty eight
In the following example, we let the static method Max3 use self:: to call the non-static method Max, and the non-static method Max calls the non-static attribute $pi through $this.
An error is reported during the run, which is the same as in the previous example 3-1-9.php, but this time the non-static method Max reports an error in static method calls to non-static properties.
This proves that the non-static method Max we defined here is automatically converted to a static method by the system.
The copy code is as follows:
The result of running the program:
one
two
The maximum value in display 99 77 188 is
Fatal error: Access to undeclared static property: Math::$pi in E:PHPProjectstest.php on line 7
Thank you for your reading, the above is the content of "how to understand static variables in PHP". After the study of this article, I believe you have a deeper understanding of how to understand static variables in PHP, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.