The difference analysis of static class and static variable usage in PHP _php tutorial

Source: Internet
Author: User
Tags parse error

Analysis of the difference between static class and static variable usage in PHP


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:

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: 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:: $country

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

Parent:: 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:: $country

Person::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:

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


example, using static variable examples, the code is as follows:

The code is as follows:

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:

function Test ()
{
static $count = 0;

$count + +;
Echo $count;
if ($count < 10) {
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.

http://www.bkjia.com/PHPjc/944486.html www.bkjia.com true http://www.bkjia.com/PHPjc/944486.html techarticle analysis of the difference between static class and static variable usage in PHP 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 the static we read this article ...

  • 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.