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

Source: Internet
Author: User
Tags constant

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, as shown in the following figure;

 

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 code is as follows: Copy code

Class Person
    {
// The following table lists the static member attributes of a person.
Public static $ myCountry = "China ";
// Var $ name; // name of a person

// This is a static member of a person.
Public static function say ()
    {
Echo "I am a Chinese user
";
    }
}

// Output static attributes
Echo Person: $ myCountry;

// Access static methods
Person: say ();

// Assign a new value to the static attribute
Person: $ myCountry = "us ";
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, as shown in the following figure;

 

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 code is as follows: Copy code

Class Person
    {  
// The following table lists the static member attributes of a person.
Public static $ myCountry = "China ";
// Var $ name; // name of a person
 
// This is a static member of a person.
Public static function say ()
    {  
Echo "I am a Chinese user
";
    }  
}  
 
// Output static attributes
Echo Person: $ myCountry;
 
// Access static methods
Person: say ();
 
// Assign a new value to the static attribute
Person: $ myCountry = "us ";
Echo Person: $ myCountry;

Class Person
    {
// The following table lists the static member attributes of a person.
Public static $ myCountry = "China ";
// Var $ name; // name of a person

// This is a static member of a person.
Public static function say ()
    {
Echo "I am a Chinese user
";
    }
}

// Output static attributes
Echo Person: $ myCountry;

// Access static methods
Person: say ();

// Assign a new value to the static attribute
Person: $ myCountry = "us ";

Echo Person: $ myCountry; because static members are created when the class is loaded for the first time, therefore, static members can be accessed by class names without objects outside the class. As mentioned above, static members are shared by every instance object of the class, so can we use objects as static members in the category? From the figure above, 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, except that self represents the class of this 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 code is as follows: Copy code

Class Person
{
// The following table lists the static member attributes of a person.
Public static $ myCountry = "China ";

// This is the static member method of a person. Access other static members through self.
Public static function say ()
    {
Echo "I am". self: $ myCountry ."";
    }
}
// Access static methods
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 code is as follows: Copy code

Class MyClass
{
// Define a constant
Const constant = 'constant value ';

Function showConstant (){
Echo self: constant. "n"; // use self for access. Do not add "$"
    }
}

Echo MyClass: constant. "n"; // use the class name for access without adding "$"

$ Class = new MyClass ();
$ Class-> showConstant ();
// 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 code is as follows: Copy code

Final class Person
{
}

Class Student extends Person
{

// The following error occurs:
Fatal error: Class Student may not inherit from final class (Person)

The final key tag method cannot be overwritten by the quilt class. It is the final version;

Class Person
{
Final function say (){}
}

Class Student extends Person
{
Function say (){}
}

// The following error occurs:
Fatal error: Cannot override final method Person: say ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.