Use of php object-oriented static, const, and final keywords

Source: Internet
Author: User
The Static keyword is used to describe the member attributes and member methods in the class. The final keyword can only be used to define the class and method. The final keyword cannot be used to define the member attributes, because final is a constant,

The Static keyword is used to describe the member attributes and member methods in the class. The final keyword can only be used to define the class and method. The final keyword cannot be used to define the member attributes, because final is the meaning of constants, we define constants in PHP to use the define () function, so we cannot use final to define member attributes.

Static keywords are used to describe member attributes and member methods in a class. where are the benefits of Static members? We previously declared that the "Person" human, in the "Person" class, if we add a property 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.

The instance code is as follows:

  1. Class Person
  2. {
  3. // The following table lists the static member attributes of a person.
  4. Public static $ myCountry = "China ";
  5. // Var $ name; // name of a person
  6. // This is a static member of a person.
  7. Public static function say ()
  8. {
  9. Echo "I am a Chinese user
  10. ";
  11. }
  12. }
  13. // Output static attributes
  14. Echo Person: $ myCountry;
  15. // Access static methods
  16. Person: say ();
  17. // Assign a new value to the static attribute
  18. Person: $ myCountry = "us ";
  19. Echo Person: $ myCountry;

"PHP5 object-oriented explanation (Gao Luofeng)" this tutorial is well written. for PHP Cainiao or laruence, it seems to be worth reading. I will attach a Word document php5 object-oriented explanation.

Use of static and const keywords

Static keywords are used to describe member attributes and member methods in a class. where are the benefits of Static members? We previously declared that the "Person" human, in the "Person" class, if we add a property 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.

The instance code is as follows:

  1. Class Person
  2. {
  3. // The following table lists the static member attributes of a person.
  4. Public static $ myCountry = "China ";
  5. // Var $ name; // name of a person
  6. // This is a static member of a person.
  7. Public static function say ()
  8. {
  9. Echo "I am a Chinese user
  10. ";
  11. }
  12. }
  13. // Output static attributes
  14. Echo Person: $ myCountry;
  15. // Access static methods
  16. Person: say ();
  17. // Assign a new value to the static attribute
  18. Person: $ myCountry = "us ";
  19. Echo Person: $ myCountry;
  20. Class Person
  21. {
  22. // The following table lists the static member attributes of a person.
  23. Public static $ myCountry = "China ";
  24. // Var $ name; // name of a person
  25. // This is a static member of a person.
  26. Public static function say ()
  27. {
  28. Echo "I am a Chinese user
  29. ";
  30. }
  31. }
  32. // Output static attributes
  33. Echo Person: $ myCountry;
  34. // Access static methods
  35. Person: say ();
  36. // Assign a new value to the static attribute
  37. Person: $ myCountry = "us ";
  38. Echo Person: $ myCountry;

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, if a static member is shared by every instance object of this class, can we use the object to access static members in the category? 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 members are used. in other object-oriented languages, for example, Java can access static members using objects, if PHP can use objects to access static members, we should try not to use them because static members are designed to access the members by class names during Project creation.

Static methods in the class can only be static attributes of the category. static methods in the class cannot be non-static members of the category. The reason is very simple, to access other members of this class in the method of this class, we need to use the reference $ this, and the reference pointer $ this represents the object that calls this method, we have mentioned that static methods do not need to be called by objects, but are accessed by class names, so there is no object at all, and there is no reference to $ this, without the $ this reference, the non-static members in the category cannot be accessed because the static members in the class can be accessed without objects, therefore, the static method in the class can only have the static attribute of the category class, that is, $ this does not exist. to access other static members in the static method, we use a special class "self "; self is similar to $ this, but self is the class that represents the static method. 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 method, that is, "self: Member attributes.

The instance code is as follows:

  1. Class Person
  2. {
  3. // The following table lists the static member attributes of a person.
  4. Public static $ myCountry = "China ";
  5. // This is the static member method of a person. access other static members through self.
  6. Public static function say ()
  7. {
  8. Echo "I am". self: $ myCountry ."";
  9. }
  10. }
  11. // Access static methods
  12. Person: say ();

You can access static members in non-static methods. of course, you can also use 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 "const" keyword, similar to # define in C. if the value of define is changed in the program, an error occurs, the access method of member attributes modified with "const" is similar to that of 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 to access it.

The instance code is as follows:

  1. Class MyClass
  2. {
  3. // Define a constant
  4. Const constant = 'constant value ';
  5. Function showConstant (){
  6. Echo self: constant. "n"; // use self for access. do not add "$"
  7. }
  8. }
  9. Echo MyClass: constant. "n"; // use the class name for access without adding "$"
  10. $ Class = new MyClass ();
  11. $ Class-> showConstant ();
  12. // Echo $ class: constant; is not allowed

Application of final keywords

This keyword can only be used to define classes and methods. The final keyword cannot be used to define member attributes. because final is a constant, we use define () to define constants in PHP () function, so you cannot use final to define member attributes.

Classes marked with final key cannot be inherited;

The instance code is as follows:

  1. Final class Person
  2. {
  3. }
  4. Class Student extends Person
  5. {
  6. }
  7. // The following error occurs:
  8. Fatal error: Class Student may not inherit from final class (Person)
  9. The final key tag method cannot be overwritten by the quilt class. it is the final version;
  10. Class Person
  11. {
  12. Final function say (){}
  13. }
  14. Class Student extends Person
  15. {
  16. Function say (){}
  17. }
  18. // The following error occurs:
  19. Fatal error: Cannot override final method Person: say ()

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.