Php object-oriented use of static, const, final keywords _ PHP Tutorial

Source: Internet
Author: User
Php targets the use of static, const, and final keywords. The Static keyword is used to describe member attributes and member methods in a class. The final keyword can only be used to define classes and methods, the Static keyword final cannot be used to describe member attributes and member methods in the class. The final keyword application 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 define constants in PHP using 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 code is as follows:

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

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? 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:

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:

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:

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 ()

The keyword "apply" can only be used to define classes and methods. you cannot use the keyword "final...

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.