Analysis of the difference between static and static variables in PHP

Source: Internet
Author: User
Tags constructor parse error php code

Static is the definition of a static object or a static variable, and what are the characteristics of the variable or class method defined by the static we'll see after we've read the relevant examples of this article.

1. Create object $ob ject = new Class () and use "->" to invoke: $ob Ject->attribute/function, provided that the variable/method is accessible.

2. Direct call class method/variable: class::attribute/function, whether static/non-static can, but there are prerequisites.

A. If it is a variable, you need the variable to be accessible.

B. If this is a method, it needs to be satisfied in addition to the method being accessible.

① if it is a static method, there is no special condition.

② if it is a non-static method, you need to change the method without using the $this, that is, not to call the Non-static variable/method, of course, there is no problem calling static variables/methods.

Then we'll look at the use of $ob ject-> .... and use class:: ... What's the difference:

1. Using $ob ject-> ..., you need to execute the constructor to create the object.

2. Use class::. Call a static method/variable and do not need to perform a constructor creation object.

3. Use class::. Call non-static methods/variables, and do not need to perform constructor creation objects.

Then the strange place came out, and since 2 and 3 are the same, what's the point of static method/variable existence?

Static statics: Declaring a class member or method static, you can access it directly without instantiating the class, and you cannot access static members of it through an object (except static methods), the static member belongs to the 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:

[Code]php Code:

01<?php

02Class person{

03//define static member properties

public static $country = "China";

05//define static member method

The public static function Mycountry () {

07//Internal access static member properties

echo "I am". Self:: $country. " People <br/> ";

09}

10}

11class Student extends Person {

The function study () {

echo "I am". Parent:: $country. " People <br/> ";

14}

15}

16//Output Member Property value

17echo Person:: $country. " <br/> "; Output: China

18$P1 = new Person ();

19//echo $p 1->country; Wrong wording

20//access to static member methods

21person::mycountry (); Output: I am a Chinese

22//static methods can also be accessed through objects:

23$p1->mycountry ();

24

Output member property value in 25//subclass

26echo Student:: $country. " <br/> "; Output: China

27$T1 = new Student ();

28$t1->study (); Output: I am a Chinese

29?>

Run the example and output:

China

I am a Chinese

I am a Chinese

China

I am a Chinese

Summary: Access static member properties or methods within a class, using self::(notice is not $slef), the code is as follows:

Slef:: $country

Slef:: Mycountry ()

To access a static member property or method of a parent class in a subclass, use Parent::(notice is not $parent), the code is as follows:

Parent:: $country

Parent:: Mycountry ()

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

Person:: $country

Person::mycountry ()

Student:: $country

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

example, declare a static variable with the following code:

[Code]php Code:

1<? Php

2 function foo () {

3 static $int = 0;//correct

4 static $int = 1+2; Wrong (as it is an ex pression)

5 Static $int = sqrt (121); Wrong (as it is an ex pression too)

6 $int + +;

7 echo $int;

8}

9?>

example, using the example of a static variable, the code is as follows:

[Code]php Code:

1<? Php

2 function Test ()

3 {

4 static $w 3sky = 0;

5 echo $w 3sky;

6 $w 3sky++;

7}

8?>

Now, each call to the Test () function will output $w 3sky value and add one.

Static variables also provide a way to handle recursive functions, recursive functions are a call to their own functions, write recursive functions to be careful, because it may be infinite recursion, you must ensure that there are adequate methods to abort recursion, the simple function of recursive count to 10, using static variable $count to determine when to stop.

example, static variables and recursive functions, the code is as follows:

[Code]php Code:

01<? Php

function Test ()

03 {

static $count = 0;

05

$count + +;

$count of the Echo;

if ($count < 10) {

Test ();

10}

One $count--;

12}

13?>

Note: Static variables can be declared according to the example above, and if you assign a value to a declaration using the result of an expression, it will cause a parse error.

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.