The difference between static class and static variable usage in PHP _php tips

Source: Internet
Author: User
Tags parse error static class

This article analyzes the difference between static and static variables 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 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 $object = new Class (), and then use "->" to invoke: $object->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 $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 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:

Copy Code code as follows:
<?php
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 <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; 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. " <br/> "; Output: China
$t 1 = new Student ();
$t 1->study (); Output: I am a Chinese
?>

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:

Copy Code code 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:

Copy Code code as follows:
Parent:: $country

Parent:: Mycountry ()

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

Copy Code code 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:

Copy Code code 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 a expression too)
$int + +;
Echo $int;
}
?>

example, using the example of a static variable, the code is as follows:
Copy Code code as follows:
? Php
function Test ()
{
static $w 3sky = 0;
echo $w 3sky;
$w 3sky++;
}
?>

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:

Copy Code code 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 example above, and if you assign a value to a declaration using the result of an expression, it will cause a parse error.

I hope this article will help you with your PHP program design.

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.