Section Seventh-Static members of a class _php tutorial

Source: Internet
Author: User
Tags php programming
/*
+-------------------------------------------------------------------------------+
| = This is haohappy read < >
| = Notes in Classes and objects Chapter
| = Translation-based + personal experience
| = Please do not reprint in order to avoid the unnecessary trouble that may occur.
| = Welcome criticism, hope and all the PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/


Section Seventh-Static members of a class

A static member of a class is different from a normal class member: A static member is not related to an instance of an object, but only to the class itself. They are used to implement the functionality and data that the class wants to encapsulate, but not the functionality and data of a particular object. Static members include static methods and static properties.

A static property contains the data to encapsulate in the class, which 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.

In the following example, we used a static property counter:: $count. It belongs to the counter class and does not belong to any counter instance. You cannot refer to it with this, but you can use self or another valid named expression. In the example, the GetCount method returns self: $count instead of counter:: $count.

The static party law implements the functionality that the class needs to encapsulate, regardless of the specific object. A static method is very similar to a global function. 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 whether the qualifier is accessed.

In 6.3 cases, GetCount is an ordinary method, called with. PHP builds 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 are no valid objects, so we should use a static method. PHP will not build this variable inside a static method, even if you call them from an object.

Example 6.7 changes the GetCount from 6.3 to a static method. The static keyword does not prevent an instance from invoking GetCount with the operator, but PHP does not establish the this variable inside the method. If you use This-> to invoke, an error will occur.

6.3 Cases refer to section fourth-examples of constructors and destructors (see previous article), and by comparison of two examples, you can get a good grasp of
The difference between a static method and a normal method.

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

Your class can also define a constant property, and you do not need to use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class.

Listing 6.7 Static Members


Copy CodeThe code is as follows: 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, __construct () executes the
$c = new Counter ();

Output 1
Print (Counter::getcount (). "
\ n ");

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

http://www.bkjia.com/PHPjc/316947.html www.bkjia.com true http://www.bkjia.com/PHPjc/316947.html techarticle / * +-------------------------------------------------------------------------------+ |= This is haohappy read corephpprogramming |= in ClassesAndObjects a chapter of the note |= translation of the main + personal ...

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