Static and const in PHP

Source: Internet
Author: User
The static keyword is described in a class where member properties and member methods are static, and where are the benefits of static members? Before we declare the "person" of the human beings, in the class "people" if we add a "human state" attribute, so that the class "man" to instantiate hundreds of or more instance objects, each object has a "state" property, If the development of the project is developed for the Chinese, then each object has a national property is the "China" other properties are different, if we make the "state" of the properties of static members, so that the country's properties in memory only one, and let these hundreds of or more objects share this property, Static members can restrict external access because static members belong to the class, are not part of any object instance, are allocated space when the class is loaded for the first time, other classes are inaccessible, only instances of the class are shared, and the members of the class can be protected to a certain extent;

From the memory point of view, we will analyze, the memory is logically divided into four segments, where the object is placed in "heap memory", the object's reference is placed in the "stack Memory", and the static member is placed in the "initialization static segment", in the first time the class is loaded, you can let each object in the heap memory is shared, such as:

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

01

02 classPerson

03 {

04 //下面是人的静态成员属性

05 publicstatic$myCountry= "中国";

06

07 // var $name; //人的名子

08

09 //这是人的静态成员方法

10 publicstaticfunctionsay() {

11 echo"我是中国人";

12 }

13 }

14

15 //输出静态属性

16 echoPerson::$myCountry;

17

18 //访问静态方法

19 Person::say();

20

21 //重新给静态属性赋值

22 Person::$myCountry= "美国";

23 echoPerson::$myCountry;

24 ?>

Because static members are created at the time the class is first loaded, it is possible to use the class name to access static members that do not require an object outside of the class, as stated above, when a static member is shared by each instance object of the class, can we use the object to access the static members of the class? As we can see, the static members are not present within each object, but each object can be shared, so if we use objects to access the members, there will be no such attribute definition, the use of object access to static members , in other object-oriented languages, For example, Java can be used to access static members of the object, if you can use the object in PHP to access static members, we also try not to use, because the static members when we do the project is to use the class name to access.

Static methods inside a class can only access static properties of the class, and static methods within the class cannot access the non-static members of the class, for the simple reason that we want to access the other members of this class in a method of this class, we need to use the $this reference, and $ This reference pointer is the object that calls this method, we say that the static method is not called by the object, but using the class name to access , so there is no object exists, there is no $this this reference, no This reference cannot access the non-static members of the class, and because the static members of the class can be accessed without objects, the static method inside the class can only access the static properties of the class, that is, $this does not exist, and in static methods we use a special class " self "; Self is similar to $this, except that self is the class that represents this static method. In a static method, you can use the class name of the class in which the method resides, or you canuse "self" to access other static members, and if there is no special case, we usually use the latter, the "Self:: member Property ".

01

02 classPerson

03 {

04 //下面是人的静态成员属性

05 publicstatic$myCountry= "中国";

06

07 //这是人的静态成员方法, 通过self访问其它静态成员

08 publicstaticfunctionsay() {

09 echo"我是". self::$myCountry;

10 }

11 }

12

13 //访问静态方法

14 Person::say();

15 ?>

It is also possible to access static members in non-static methods, but you cannot use the "$this" reference, or use the class name or "self: the form of a member property."

Const is a keyword that defines constants, and in PHP defines constants using the "define ()" function, but defining constants in a class uses the keyword "const", similar to the C # # If you change its value in a program, an error occurs with the " The const "Decorated member property is accessed in much the same way as the members of the" static "adornment, using the" self "keyword in the method using the" Class name ". However, you do not have to use the "$" symbol and you cannot use objects to access it.

01

02 classMyClass

03 {

04 //定义一个常量constant

05 constconstant = 'constant value';

06

07 functionshowConstant() {

08 echoself::constant . " ";// 使用self访问,不要加“$”

09 }

10 }

11

12 echoMyClass::constant ." "; //使用类名来访问,也不加“$”

13

14 $class= newMyClass();

15 $class->showConstant();

16 // echo $class::constant; 是不允许的

17 ?>

The above describes the static and const PHP, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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