Php object-oriented full introduction (10) use of finalstaticconst keywords _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented full strategy (10) use of the finalstaticconst keyword. 14. the application of the final 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. 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.
Final indicates a constant. we define a constant in PHP and use the define () function, so we cannot use final.
Define member attributes.
Classes marked with final key cannot be inherited;
Code snippet
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;
Code snippet
Class Person {
Final function say (){
}
}
Class Student extends Person {
Function say (){
}
}
The following error occurs:
Fatal error: Cannot override final method Person: say ()
15. 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 "Person's country"
In this way, the "Person" class instance is used to generate hundreds or more instance objects.
It has the property of "country". if the development project is developed for the Chinese, then every object contains
The attribute of one country is "China" and other attributes are different. if we make the attribute of "country" Static
In this way, the country has only one attribute in the memory, and the hundreds or more objects share this attribute,
Static members can restrict external access because static members belong to classes and do not belong to any object instance. yes
The space allocated when the class is loaded for the first time. other classes cannot be accessed. they are only shared with the instance of the class.
Degree of protection for such members;
From the memory perspective, the memory is logically divided into four sections, where the object is placed in the heap memory.
Objects are referenced in the stack memory, and static members are put in the initialized static segments.
When loaded, each object in the heap memory can be shared, for example;
= 700) window. open ('/upload/20090930223111418.gif'); "src =" http://www.bkjia.com/uploads/allimg/131016/0919403415-0.gif "onload =" if (this. width> '200') this. width = '000000'; if (this. height> '20140901') this. height = '000000'; "border = 0>
Static variables of the class, which are very similar to global variables and can be shared by all class instances. the static methods of the class are the same.
Is similar to a global function.
Code snippet

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


Because static members are created when the class is loaded for the first time, the class is used outside the class without an object.
The static member can be accessed by the name. as mentioned above, the static member is shared by every instance object of this class.
Can we use an object to access static members in the category? We can see that static members are not
Each object exists internally, but each object can be shared. Therefore, if an object is used to access members
If this attribute definition does not exist and the object cannot access static members, in other object-oriented languages, such
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 methods in the class cannot be non-static
The reason is very simple. to access other members of this class in the method of this class, we need to use $ this
This reference, and $ this reference pointer represents the object that calls this method. we have mentioned that static methods do not need objects.
The class name is used for access, so there is no object at all, there is no reference for $ this, no
$ This reference does not allow non-static members in the category, and because static members in the class do not need objects.
So the static method in the class can only be the static attribute of the category class, that is, $ this does not exist, in the static method
To access other static members, we use a special class "self". self is similar to $ this, but self is representative.
The class of the static method. So 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:
Staff attributes.
Code snippet

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


Can I access static members in non-static methods? of course, this is also acceptable, but it cannot use "$ this"
You must also use the class name or "self: member attribute form" for reference ".
Const is a key word for defining constants. Defining constants in PHP uses the "define ()" function,
The key word "const" is used to define constants in the class. it is similar to # define in C if it is changed in the program.
An error occurs when the access method of the member attribute modified by "const" and the member modified by "static" are used.
The access method is similar. the "class name" is also used, and the "self" keyword is used in the method. But you do not need to use "$"
And cannot be accessed using objects.
Code snippet

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

The application of the http://www.bkjia.com/PHPjc/320633.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320633.htmlTechArticle14.final keyword this keyword can only be used to define the class and the definition method, can not use the final keyword to define the member attribute, because final is the meaning of the constant, we in...

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.