Static and const in php

Source: Internet
Author: User
: This article mainly introduces static and const in php. For more information about PHP tutorials, see. Static keywords are used to describe member attributes and member methods in a class. What are the benefits of static members? We previously declared that the "Person" human beings, in the "Person" class, if we add the attribute of "Person's country, in this way, the "Person" class instance is used to produce hundreds or more instance objects. each object has the "country" attribute, if the development project is developed for the Chinese people, then each object has a country attribute that is "China". other attributes are different, if we make the "country" attribute a static member, then there will be only one country attribute in the memory, and this attribute will be shared by hundreds or more objects, static members can restrict external access. Because static members belong to classes and do not belong to any object instance, they are allocated space when the class is loaded for the first time, other classes cannot be accessed. they only share instances of the class and can protect the members of the class to a certain extent;

From the memory perspective, we can analyze that the memory is logically divided into four sections, where the object is placed in the heap memory, and the object reference is placed in the stack memory, static members are placed in the "initialize static segments". when the class is loaded for the first time, each object in the heap memory can be shared, for example:

Static variables of the class are very similar to global variables and can be shared by all class instances. static methods of the class are also the same, similar to global functions.

01

02 classPerson

03 {

04 // The following table lists the static member attributes of a person.

05 publicstatic$myCountry= "China";

06

07 // Var $ name; // name of a person

08

09 // This is a static member of a person.

10 publicstaticfunctionsay() {

11 echo"I am a Chinese";

12 }

13 }

14

15 // Output static attributes

16 echoPerson::$myCountry;

17

18 // Access static methods

19 Person::say();

20

21 // Assign a new value to the static attribute

22 Person::$myCountry= "USA";

23 echoPerson::$myCountry;

24 ?>

Because static members are created when the class is loaded for the first time, static members can be accessed without an object outside the class by using the class name. as mentioned above,Static members are shared by every instance object of this class.So can we use an object to access static members in the category class? We can see that static members do not exist within each object, but each object can be shared. Therefore, if we use an object to access members, this attribute is not defined,Objects that cannot access static membersIn other object-oriented languages, for example, Java can use objects to access static members. If PHP can use objects to access static members, we also try not to use it because static members are designed to use class names to access the project.

The static methods in the class can only be the static attributes of the category.The static method in the class cannot be a non-static member of the class, the reason is very simple, we want to access other members of this class in the class method, we need to use the $ this reference, and the $ this reference pointer represents the object that calls this method.Static methods are accessed using class names instead of object calls.So there is no object at all, and no $ this reference is available. without $ this reference, non-static members in the category cannot be called, because the static members in the class can be accessed without objects, the static methods in the class can only be the static attributes of the category, even if $ this does not exist, to access other static members in a static method, we use a special class "self"; self is similar to $ this, but self represents the class where the static method is located. Therefore, in a static method, you can use the "class name" of the class where this method is located, or use "self" to access other static members. if there are no special circumstances, we usually use the latter, that is,"Self: Member attributes.

01

02 classPerson

03 {

04 // The following table lists the static member attributes of a person.

05 publicstatic$myCountry= "China";

06

07 // This is the static member method of a person. access other static members through self.

08 publicstaticfunctionsay() {

09 echo"I am". self::$myCountry;

10 }

11 }

12

13 // Access static methods

14 Person::say();

15 ?>

Can I access static members in non-static methods? of course, this is also acceptable, but it cannot be referenced using "$ this", or the class name or "self :: the form of member attributes ".

Const is a key word for defining constants. Defining constants in PHP uses the "define ()" function, but defining constants in a class uses the keyword "const, similar to # define in C. if the value of define is changed in the program, an error occurs, 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.

01

02 classMyClass

03 {

04 // Define a constant

05 constconstant = 'constant value';

06

07 functionshowConstant() {

08 echoself::constant . " ";// Use self for access. do not add "$"

09 }

10 }

11

12 echoMyClass::constant ." "; // Use the class name for access without adding "$"

13

14 $class= newMyClass();

15 $class->showConstant();

16 // Echo $ class: constant; is not allowed

17 ?>

The above introduces static and const in php, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.