Static and const keyword Usage Analysis in php, staticconst

Source: Internet
Author: User

Static and const keyword Usage Analysis in php, staticconst

This example describes the usage of static and const keywords in php. We will share this with you for your reference. The details are as follows:

The member attributes and member functions described by the static keyword in the class are static.

Static members can restrict external access because static members belong to Classes rather than any object instance.

From the memory perspective, objects are placed in the heap memory, objects are referenced in the stack memory, and static members are placed in the initialized static segments, added when the class is loaded for the first time. Share all objects in the memory. As shown in:

<? Phpclass Person {public static $ myCountry = "China"; public static function say () {echo "my country is :". self: $ myCountry. "<br>" ;}}// output Static Property echo Person: $ myCountry. "<br>"; // call the static method Person: say (); // modify the static property Person: $ myCountry = "China-Jiangsu"; echo Person :: $ myCountry. "<br>";?>

Output result:

China and my motherland are: China-Jiangsu

The static method in the class can only be the static attribute of the category class. The static method in the class cannot be a non-static member of the class. We use self to perform static attributes in the category. Self is similar to this, except that self represents the class where the static method is located. this is similar, but self represents the class where the static method is located. this references the pointer and represents the object that calls this method. Static methods do not need to be called by objects, so all of them do not have this reference. There is no reference for this. Without this, other member attributes in the class cannot be called.

Const is a key word that defines constants. In the class, const is often used to define constants. The access method of the member Attribute Modified with "const" is similar to that of the member access modified with "static". The "Class Name" is also used, and the "self" keyword is used in the method. However, you do not need to use the "$" symbol or use an object for access.

<? Phpclass MyClass {const constant = 'constant value'; function showConstant () {// call a constant in the method, without $ echo self: constant. "<br>" ;}}// class directly called, no $ echo MyClass: constant. "<br>"; $ class = new MyClass (); $ class-> showConstant ();?>

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.