Differences between static and static variables in php _ PHP Tutorial

Source: Internet
Author: User
Differences between static and static variables in php. Static is to define a static object or 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. creating an object $ static is to define a static object or a static variable. after reading the related examples of static variables or class methods, we can see that.

1. create an object $ object = new Class () and then use "->" to call: $ object-> attribute/function, provided that the variable/method is accessible.
2. directly call the class method/variable: class: attribute/function, either static or non-static. However, there are prerequisites:
A. If it is A variable, the variable must be accessible.
B. If it is a method, in addition to the method that can be accessed, it also needs to meet the following requirements:
B1) if it is a static method, there are no special conditions;
B2) 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
If the declared class member or method is static, you can directly access the static member without instantiating the class. you 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.

Example:

The code is as follows:

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, for example:

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), for example:

The code is as follows:
Parent: $ country
Parent: myCountry ()

For external access, the static member attributes and methods are class name/subclass name:, for example:

The code is as follows:
Person: $ country
Person: myCountry ()
Student: $ country

However, static methods can also be accessed through common objects.


Static variables declared in the example

The code is as follows:

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 of static variables

The code is as follows:
Function Test ()
{
Static $ w3sky = 0;
Echo $ w3sky;
$ W3sky ++;
}
?>

Now, every time you call the Test () function, the value of $ w3sky is output and added with one.


Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion. The simple function calculates 10 recursively and uses the static variable $ count to determine when to stop:

Static variables and recursive functions

The code is as follows:

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 a value is assigned to the expression result in the declaration, the parsing error occurs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.