Section seventh static members of class [7]_php Foundation

Source: Internet
Author: User

A static member of a class differs from a general class member: A static member is independent of an instance of an object, and is related only to the class itself. They are used to implement the functionality and data that the class encapsulates, but do not include the functionality and data of a particular object. Static members include static methods and static properties.

Static properties are contained in the data to be encapsulated in the class and can be shared by instances of all classes. In fact, the static properties of a class are very similar to the global variables of a function, except that they belong to a fixed class and restrict access methods.

We use a static property in the following example counter:: $count. It belongs to the counter class and does not belong to any counter instance. You cannot use this to refer to it, but you can use self or other valid named expressions. In the example, the GetCount method returns self:: $count, not counter:: $count.

The static square law implementation class needs to encapsulate the functionality, regardless of the specific object. Static methods are very similar to global functions. A static method can have full access to the properties of a class, or it can be accessed by an instance of an object, regardless of the qualifying language of the access.

In 6.3 cases, GetCount is a common method, called by->. PHP creates a This variable, although the method is not used. However, GetCount does not belong to any object. In some cases, we even want to call it when there is no valid object, then we should use a static method. PHP will not create this variable inside static methods, even if you call them from an object.

Example 6.7 changed GetCount by 6.3 to a static method. The static keyword cannot prevent an instance from invoking GetCount with the-> operator, but PHP will not create this variable inside the method. If you use the this-> to invoke, there will be an error.

6.3 Cases refer to the fourth section--Examples of constructors and destructors (see previous), and by comparing them with two examples, you can well grasp
The difference between a static method and a common method.

You can write a method to show whether it is being invoked statically or statically, by determining whether this is established. Of course, if you use the static keyword, no matter how it is invoked, this method is always static.

Your class can also define constant properties without using the public static, just use the const keyword. Constant properties are always static. They are properties of a class, not an object that instantiates the class.

Listing 6.7 Static Members

<?php
Class Counter
{
private static $count = 0;
Const VERSION = 2.0;

function __construct ()
{
Self:: $count + +;
}

function __destruct ()
{
Self:: $count-;
}

static function GetCount ()
{
Return self:: $count;
}
};
Creates an instance, the __construct () executes the
$c = new Counter ();

Output 1
Print (Counter::getcount (). "<br>n");

Version properties of the output class
Print ("Version used:".) Counter::version. "<br>n");
?>

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.