Home >Backend Development >PHP Tutorial >Analysis of the difference between static classes and static variables in php, php variables_PHP tutorial
This article analyzes the difference between the usage of static classes and static variables in PHP with examples. Share it with everyone for your reference. The specific analysis is as follows:
Static is to define a static object or static variable. We will know the characteristics of variables or class methods defined by static after reading the relevant examples in this article.
1. Create object $object = new Class(), and then use "->" to call: $object->attribute/function, provided that the variable/method is accessible.
2. Directly call class methods/variables: class::attribute/function, whether it is static or non-static, but there are prerequisites.
A. If it is a variable, the variable needs to be accessible.
B. If it is a method, in addition to the method being accessible, it also needs to meet the requirements.
① If it is a static method, there are no special conditions.
② If it is a non-static method, you need to change the method so that $this is not used, that is, non-static variables/methods are not called. Of course, there is no problem in calling static variables/methods.
Then let’s look at the difference between using $object->… and using class::…:
1. Using $object->…, you need to execute the constructor to create the object.
2. Use class::... to call static methods/variables without executing the constructor to create objects.
3. Use class::... to call non-static methods/variables, and there is no need to execute the constructor to create objects.
Then the strange thing comes out. Since 2 and 3 are the same, what is the point of the existence of static methods/variables?
Static: Declaring class members or methods as static means they can be accessed directly without instantiating the class. Static members cannot be accessed through an object (except for static methods). Static members belong to the class and do not belong to any object instance. , but all object instances of the class can be shared.
Example, the code is as follows:
Summary: To access static member properties or methods inside a class, use self:: (note not $slef), the code is as follows:
slef:: myCountry()
To access the static member properties or methods of the parent class in a subclass, use parent:: (note not $parent), the code is as follows:
parent:: myCountry()
External access to static member attributes and methods is class name/subclass name::, the code is as follows:
Person::myCountry()
Student::$country
But static methods can also be accessed through ordinary objects.
Example, declare a static variable, the code is as follows:
Static variables also provide a way to deal with recursive functions. A recursive function is a function that calls itself. Be careful when writing a recursive function, because it may recurse infinitely. You must ensure that there is a sufficient method to terminate the recursion. Let’s take a look at this simple function that recursively counts to 10, using the static variable $count to determine when to stop.
Example, static variables and recursive functions, the code is as follows:
I hope this article will be helpful to everyone’s PHP programming design.