Differences between static classes and static variables in php

Source: Internet
Author: User
Differences between static classes and static variables in php: php variables. Differences between static class and static variable usage in php. This article describes the differences between static class and static variable usage in php. Share it with you for your reference. The specific analysis is as follows:

This article analyzes the differences between static classes and static variables in php. Share it with you for your reference. The specific analysis is as follows:

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 $ object = new Class () and then use "->" to call: $ object-> 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 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: 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:

The code is as follows:

<? Php
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. the code is as follows:

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:

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:

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:

The code is as follows:

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


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

The code is as follows:

<? PHP
Function Test ()
{
Static $ w3sky = 0;
Echo $ w3sky;
$ W3sky ++;
}
?>


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:

The code is as follows:

<? PHP
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 you assign a value to the expression result in the declaration, parsing errors will occur.

I hope this article will help you with php programming.

In this article, we analyze the differences between static classes and static variables in php. Share it with you for your reference. The specific analysis is as follows...

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.