This article mainly introduces the differences between static and static variables in php, and analyzes in detail the definitions, functions, and usage skills of static and static variables in php in the form of examples, for more information, see
This article mainly introduces the differences between static and static variables in php, and analyzes in detail the definitions, functions, and usage skills of static and static variables in php in the form of examples, for more information, see
This article analyzes the differences between static classes and static variables in php. Share it with you for your reference. The specific analysis is as follows:
Static is to define a static object or a static variable. After reading the related examples in this article, we will be able to understand the features of static variables or class methods.
1. Create an object $ object = new Class () and then use "->" to call: $ object-> attribute/function, provided that the variable/method is accessible.
2. You can directly call the class method/variable: class: attribute/function, either static or non-static, but there are prerequisites.
A. If it is A variable, the variable is accessible.
B. If it is a method, in addition to the method that can be accessed, it also needs to be satisfied.
① If it is a static method, there are no special conditions.
② If it is a non-static method, $ this is not used in the method to be modified, that is, non-static variables/methods are not called. Of course, there is no problem in calling static variables/methods.
Then let's take a look at using $ object->... And use class ::... Are there any differences:
1. Use $ object->... , You need to execute the constructor to create an object.
2. Use class ::... Call static methods/variables without executing constructors to create objects.
3. Use class ::... Call non-static methods/variables, and do not need to execute constructors to create objects.
Then it came out in a strange way. Since both 2 and 3 are the same, what is the significance of static methods/variables?
Static: declares that a class member or method is static. You can directly access the static member without instantiating the class and cannot access the static member through an object (except for static methods ), static members belong to the class and do not belong to any object instance, but the object instances of the class can be shared.
The Code is as follows:
The Code is as follows:
<? Php
Class Person {
// Define static member attributes
Public static $ country = "China ";
// Define the static member Method
Public static function myCountry (){
// Static member attributes for internal access
Echo "I am". self: $ country. "person
";
}
}
Class Student extends Person {
Function study (){
Echo "I am". parent: $ country. "person
";
}
}
// Output the member property value
Echo Person: $ country ."
"; // Output: China
$ P1 = new Person ();
// Echo $ p1-> country; // incorrect syntax
// Method for accessing static members
Person: myCountry (); // output: I am a Chinese
// Static methods can also be accessed through objects:
$ P1-> myCountry ();
// Outputs the member attribute values in the subclass.
Echo Student: $ country ."
"; // Output: China
$ T1 = new Student ();
$ T1-> study (); // output: I am a Chinese
?>
Run this example and output:
China
I am a Chinese
I am a Chinese
China
I am a Chinese
Summary: Use self: (Note not $ slef) to access static member attributes or methods within the class. The Code is as follows:
The Code is as follows:
Slef: $ country
Slef: myCountry ()
When the subclass accesses the static member attributes or methods of the parent class, use parent: (Note not $ parent). The Code is as follows:
The Code is as follows:
Parent: $ country
Parent: myCountry ()
For external access, the static member attributes and methods are class name/subclass name:, the Code is as follows:
The Code is as follows:
Person: $ country
Person: myCountry ()
Student: $ country
However, static methods can also be accessed through common objects.
The code for declaring static variables is as follows:
The Code is as follows:
<? PHP
Function foo (){
Static $ int = 0; // correct
Static $ int = 1 + 2; // wrong (as it is an expression)
Static $ int = sqrt (121); // wrong (as it is an expression too)
$ Int ++;
Echo $ int;
}
?>
For example, the Code for using static variables is as follows:
The Code is as follows:
<? PHP
Function Test ()
{
Static $ w3sky = 0;
Echo $ w3sky;
$ W3sky ++;
}
?>
Now, every time you call the Test () function, the $ w3sky value will be output and added.
Static variables also provide a method for processing recursive functions. recursive functions are called to call their own functions. Be careful when writing recursive functions, because they may be infinitely recursive, make sure there are sufficient methods to stop recursion. The simple function recursively counts to 10 and uses the static variable $ count to determine when to stop.
For example, static variables and recursive functions, the Code is as follows:
The Code is as follows:
<? PHP
Function Test ()
{
Static $ count = 0;
$ Count ++;
Echo $ count;
If ($ count <10 ){
Test ();
}
$ Count --;
}
?>
Note: static variables can be declared according to the preceding example. If you assign a value to the expression result in the declaration, parsing errors will occur.
I hope this article will help you with php programming.