Static in PHP

Source: Internet
Author: User

A static member is a class variable that you can think of as belonging to an entire class and not to an instance of a class. Unlike a typical instance variable, a static member retains only one variable value, and the value of that variable is valid for all instances, that is, all instances share this member.

$this represents only the current instance of the class, and self:: Represents the class itself, which cannot be used in code outside of the class, and it does not recognize its position in the inheritance tree hierarchy. That is, when you use the self scope in an extension class, self can call the method declared in the base class, but it always calls methods that have been overridden in the extension class. Unlike $this, when you use a static variable, you must add the $ symbol after the scope qualifier.

In an extension class, a method defined in a base class is invoked using the parent scope, in the case where the method of the base class is overridden. A static member can also belong to the parent class only. If you declare a member in both the subclass and the parent class, you can also use Parant:: To access the variables in the parent class in the subclass. In this case, the static members of the parent class and the child class are saved with a different value.

You can statically access a member by writing the name of the class on the left side of the: operator to avoid creating an instance of the class. Not only does it omit the code that instantiates the class, but it is also more efficient, because each instance of the class consumes a fraction of the system resources.

When using the: operator to access member variables, you need to pay attention to the use of the $ symbol again. Because PHP does not currently support the use of dynamic static variables, which means that mutable static variables are not supported. When using the $this-> $var, the member being accessed is the value of the variable contained in the $var. Instead of accessing a variable without the $ symbol, you are actually looking for a constant of the class, and the constant cannot be accessed through $this.

Static in PHP6: scope makes it unnecessary for us to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static:: This qualifier calculates the members of the last class on the inheritance hierarchy immediately before the code executes. One process is called deferred binding, which allows us to override a static variable in a subclass, and can also ask the final member from a function declared in the parent class.

Sometimes it may be necessary to create fields and methods that are shared by all class instances that relate to all class instances, but cannot be called by any particular object. For example, suppose you want to write a class that tracks the number of page visitors. You don't want to reset the number of visitors to 0 each time the class is instantiated, you can set the field to the static scope:

<?php
Class visitors
{
private static $visitors = 0;
function __construct ()
{
Self:: $visitors + +;
}
static function Getvisitors ()
{
Return self:: $visitors;
}
}
/* Instantiate the visitors class. */
$visits = new visitors ();
Echo visitors::getvisitors (). " <br/> ";
/* Instantiate another visitors class. */
$visits 2 = new visitors ();
Echo visitors::getvisitors (). " <br/> ";
?>

Program Run Result:

1

2

Because the $visitors field is declared static, any changes to its value are reflected in all instantiated objects. Also note that static fields and methods should be referenced using the Self keyword and the class name, rather than through the this and the arrow operators. This is because it is not possible to refer to a static field using the "normal" method, which results in a syntax error.

You cannot use $this in a class to refer to a static field.

static variables

A static variable is a variable that exists only in the scope of a function, but after the execution of the function, the value of the variable is not lost, that is, the variable will still remember the original value the next time the function is called. To define a variable as static, simply precede the variable with the static keyword.

In a class, the static keyword has two main uses, one for defining static members and one for defining static methods. Within a class, you can use the Scope qualifier (::) To access variables of different levels of scope.

Static methods

There is an important difference between static and non-static methods: When you call a static method, you no longer need to have an instance of the class.

Static and non-static methods use the principle: first, if a method does not contain the $this variable, it should be a static method, if you do not need an instance of the class, you might also want to use a static class, so that the work of instantiating the class can be avoided. Also, you cannot use the $this variable in a static method, because the static method does not belong to a particular instance.

Static in PHP

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.