_php tutorial for static static class and static variable usage in PHP

Source: Internet
Author: User
Tags parse error
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 there are prerequisites:
A. If it is a variable, it needs to be accessible.
B. In the case of a method, in addition to the method's accessibility, it is necessary to satisfy:
B1) If it is a static method, there is no special condition;
B2) If the method is non-static, it is not necessary 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 static method/variable, do not need to execute constructor to create 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 statics
Declaring a class member or method as static can be accessed directly without instantiating a class, and cannot access static members through an object except static methods. Static members belong to a class and do not belong to any object instance, but object instances of the class can be shared.

Example:

The code is as follows Copy Code

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
To access static member properties or methods inside a class, use self:: (note not $slef), such as:

The code is as follows Copy Code
Slef:: $country
Slef:: Mycountry ()

To access the parent class static member property or method in the subclass, use Parent:: (note not $parent), such as:

The code is as follows Copy Code
Parent:: $country
Parent:: Mycountry ()

External access static member properties and methods are class name/subclass name::, such as:

The code is as follows Copy Code
Person:: $country
Person::mycountry ()
Student:: $country

However, static methods can also be accessed by means of ordinary objects.


Example declares a static variable

The code is as follows Copy Code

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;
}
?>

Examples of using static variables

The code is as follows Copy Code
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. A recursive function is a function that calls itself. Be careful when writing recursive functions, because they can be recursive indefinitely. You must ensure that there is sufficient method to abort the recursion. This simple function recursively counts to 10, using a static variable $count to determine when to stop:

Example static variables and recursive functions

The code is as follows Copy Code

function Test ()
{
static $count = 0;

$count + +;
Echo $count;
if ($count < 10) {
Test ();
}
$count--;
}
?>

Note: Static variables can be declared according to the example above. If you assign a value to a declaration with the result of an expression, it causes a parse error.

More about PHP static variable usage


http://www.bkjia.com/PHPjc/444700.html www.bkjia.com true http://www.bkjia.com/PHPjc/444700.html techarticle 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 an object $ ...

  • Related Article

    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.