The usage and difference of static and static variables in PHP

Source: Internet
Author: User
Tags parse error php class
This article mainly introduces the difference between static class and static variable usage in PHP, analyzes the definition, function and use skill of static class and static variable in PHP in detail, and needs friends to refer to

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:

The code is as follows:

<?php Class person{     //define static member properties public     static $country = "China";     Define static member method public static     function Mycountry () {         //internal access static member property         echo "I am". Self:: $country. " People <br/> ";     } } class Student extends person {     function study () {         echo "I am". Parent:: $country. " People <br/> ";     } }//Output member property value echo Person:: $country. " <br/> ";  Output: China $p 1 = new person (); echo $p 1->country;   Error notation//Access static Member Method Person::mycountry ();   Output: I am Chinese//static methods can also be accessed by objects: $p 1->mycountry ();  Output member property values in subclasses echo Student:: $country. " <br/> "; 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:

The code is as follows:

Slef:: $countryslef:: 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:

The code is as follows:

Parent:: $countryparent:: Mycountry ()

External access static member properties and methods for the class name/subclass Name::, the code is as follows:

The code is as follows:

Person:: $countryPerson:: Mycountry () Student:: $country

However, static methods can also be accessed by means of ordinary objects.
example, declare a static variable with the following code:

The code is as follows:

<? PHP    function foo () {    static $int = 0;//correct    static $int = 1+2;   Wrong (as it is a 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:

The code is 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:

The code is as follows:

<? PHP    function Test ()    {    static $count = 0;        $count + +;    echo $count;    if ($count <) {    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.

In PHP, there are two ways to access a class's methods/variables:
1. Create the object $object = new Class (), and then use the "--" Call: $object->attribute/function If the variable/method is accessible.
2. Call the class method/variable directly: class::attribute/function, either static/non-static. However, there are prerequisites:
A. If it is a variable, it needs to be accessible.
B. If it is a method, in addition to the method can be accessed, but also need to satisfy:
B1) If it is a static method, there is no special condition;
B2) If the method is non-static, it needs to be changed without using $this, that is, the non-static variable/method is called, of course, the static variable method is not a problem.

Then we'll look at the use of $object-> and use class:: ... What's 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? The
difference is still obvious, as follows:
1. Static variables
Static members retain only one variable value, and this variable value is valid for all instances, that is, all instances share this member.
2. Static methods
Static methods can be used directly with the class:: ... Instead of static methods that require certain restrictions to use class::. Method call, as described earlier in

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.