The difference between static class and static variable usage in PHP, PHP variable
This paper analyzes the difference between static class and static variable usage in PHP. Share to everyone for your reference. The specific analysis is as follows:
Static is the definition of a static object or a static variable, and what are the characteristics of a variable or class method that is defined by static? We've seen the relevant examples of this article.
1. Create the object $object = new Class (), and then use the "-and" call: $object->attribute/function, provided the variable/method is accessible.
2. Call the class method/variable directly: class::attribute/function, either static/non-static, but with prerequisites.
A. If it is a variable, it needs to be accessible.
B. If it is a method, it needs to be met in addition to the method's accessibility.
① if it is a static method, there is no special condition.
② if the method is non-static, you do not need to use $this, that is, the non-static variable/method is not called, of course, there is no problem in calling the static variable/method.
Then we'll look at the use of $object-> and use class:: ... What is the difference:
1. Use $object->.., you need to execute the constructor to create the object.
2. Use class:: ... Call a static method/variable without executing the constructor to create the object.
3. Use class:: ... Call a non-static method/variable and do not need to execute the constructor to create the object.
Then the strange place came out, since 2 and 3 are the same, what is the meaning of static method/variable existence?
Static: Declares that a class member or method is static, can be accessed without instantiating a class, cannot access static members through an object (except static methods), a static member belongs to a class and does not belong to any object instance, but the object instance of the class can be shared.
example, the code is as follows:
Copy the Code code as follows: <?php
Class person{
Defining static member properties
public static $country = "China";
Defining static Member Methods
public static function Mycountry () {
Internal access static member properties
echo "I am." Self:: $country. " People
";
}
}
Class Student extends Person {
Function study () {
echo "I am". Parent:: $country. " People
";
}
}
Output member Property value
echo Person:: $country. "
"; Output: China
$p 1 = new person ();
Echo $p 1->country; Wrong wording
accessing static Member Methods
Person::mycountry (); Output: I am a Chinese
Static methods can also be accessed through objects:
$p 1->mycountry ();
Output member property values in subclasses
echo Student:: $country. "
"; Output: China
$t 1 = new Student ();
$t 1->study (); Output: I am a Chinese
?>
To run the example, output:
China
I am a Chinese
I am a Chinese
China
I am a Chinese
Summary: Accessing static member properties or methods inside a class, using self::(note is not $slef), the code is as follows:
Copy the Code code as follows: Slef:: $country
Slef:: Mycountry ()
To access the parent class static member property or method in the subclass, use Parent::(note is not $parent), the code is as follows:
Copy the Code code as follows: Parent:: $country
Parent:: Mycountry ()
External access static member properties and methods for the class name/subclass Name::, the code is as follows:
Copy the Code code as follows: Person:: $country
Person::mycountry ()
Student:: $country
However, static methods can also be accessed by means of ordinary objects.
example, declare a static variable with the following code:
Copy the Code code 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;
}
?>
example, using static variable examples, the code is as follows:
Copy the Code code as follows: <? Php
function Test ()
{
static $w 3sky = 0;
echo $w 3sky;
$w 3sky++;
}
?>
Now, each call to the Test () function outputs the value of $w 3sky and adds one.
Static variables also provide a way to handle recursive functions, which is a function of calling itself, be careful when writing recursive functions, because there may be infinite recursion, you must ensure there is sufficient method to abort recursion, this simple function recursive count to 10, use static variable $count to determine when to stop.
examples, static variables and recursive functions, the code is as follows:
Copy the Code code as follows: <? Php
function Test ()
{
static $count = 0;
$count + +;
Echo $count;
if ($count < 10) {
Test ();
}
$count--;
}
?>
Note: A static variable can be declared in the example above, and if it is assigned a value in the declaration with the result of an expression, it will result in a parse error.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/943418.html www.bkjia.com true http://www.bkjia.com/PHPjc/943418.html techarticle the difference between static class and static variable usage in PHP, PHP variables This paper analyzes the difference between static class and static variable usage in PHP. Share to everyone for your reference. The specific analysis is as follows ...