Differences between static classes and static variables in php

Source: Internet
Author: User
Analysis of differences between static classes and static variables in php. Differences between static class and static variable usage in php parsing static is to define a static object or static variable, what are the features of static variables or class methods? after reading this article, I will explain the differences between static and static variables in php.

Static is to define a static object or a 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. create an object $ ob ject = new Class () and then use "->" to call: $ ob ject-> attribute/function, provided that the variable/method is accessible.

2. you can directly call the class method/variable: class: attribute/function, either static or non-static, but there are prerequisites.

A. If it is A variable, the variable is accessible.

B. If it is a method, in addition to the method that can be accessed, it also needs to be satisfied.

① If it is a static method, there are no special conditions.

② 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 $ ob ject->... And use class ::... Are there any differences:

1. use $ ob ject->... , 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: declares that a class member or method is static. you can directly access the static member without instantiating the class and 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.

The code is as follows:

[Code] php code:

01

02 Class Person {

03 // define static member attributes

04 public static $ country = "China ";

05 // define the static member method

06 public static function myCountry (){

07 // internal access to static member attributes

08 echo "I am". self: $ country. "person
";

09}

10}

11 class Student extends Person {

12 function study (){

13 echo "I am". parent: $ country. "person
";

14}

15}

16 // output member attribute values

17 echo Person: $ country ."
"; // OUTPUT: China

18 $ p1 = new Person ();

19 // echo $ p1-> country; // incorrect syntax

20 // method for accessing static members

21 Person: myCountry (); // output: I am a chinese

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

23 $ p1-> myCountry ();

24

25 // specify the member attribute value in the subclass.

26 echo Student: $ country ."
"; // OUTPUT: China

27 $ t1 = new Student ();

28 $ t1-> study (); // output: I am a chinese

29?>

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. 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). The code is as follows:

Parent: $ country

Parent: myCountry ()

For external access, the static member attributes 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 through common objects.

The code for declaring static variables is as follows:

[Code] php code:

1

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

For example, the code for using static variables is as follows:

[Code] php code:

1

2 function Test ()

3 {

4 static $ w3sky = 0;

5 echo $ w3sky;

6 $ w3sky ++;

7}

8?>

Now, every time you call the Test () function, the $ w3sky value will be output and added.

Static variables also provide a method for processing recursive functions. recursive functions are called to call their own functions. be careful when writing recursive functions, because they may be infinitely recursive, make sure there are sufficient methods to stop recursion. The simple function recursively counts to 10 and uses the static variable $ count to determine when to stop.

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

[Code] php code:

01

02 function Test ()

03 {

04 static $ count = 0;

05

06 $ count ++;

07 echo $ count;

08 if ($ count <10 ){

09 Test ();

10}

11 $ count --;

12}

13?>

Note: static variables can be declared according to the preceding example. if you assign a value to the expression result in the declaration, parsing errors will occur.

Define static is to define a static object or a static variable. after reading about the characteristics of static variables or class methods...

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.