PHP Object-oriented All-finalstaticconst keyword usage

Source: Internet
Author: User
Application of 14.final Keywords
This keyword can only be used to define classes and define methods, and it is not possible to use the final keyword to define member properties because
For final is the meaning of constants, we define constants in PHP using the Define () function, so you cannot use final to
Define member properties.
Classes that use the final key tag cannot be inherited;
Code Snippets
Final class person{
... ...
}
Class Student extends person{
}
The following error will appear:
Fatal Error:class Student may not inherit from final Class (person)
The method of using the final key tag cannot be overridden by the quilt class, which is the final version;
Code Snippets
Class person{
Final function say () {
}
}
Class Student extends person{
function say () {
}
}
The following error will appear:
Fatal Error:cannot Override final Method Person::say ()
Use of 15.static and const keywords
The static keyword is described in a class where member properties and member methods are static; Where are the benefits of static membership?
In front of us, we declared "person" human beings, in the category "People" if we add a "human state"
attribute, so that hundreds of or more instance objects are instantiated with the class "person", each of which
There is a "country" attribute, if the development of the project is developed for the Chinese, then each object inside is
There is a property of a country that is "China" other attributes are different if we make the "country" attribute static
Member, so that the properties of the country are only one in memory, and let the 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 instances, and are
When the class is loaded for the first time, the space allocated, other classes are inaccessible, only the instance of the class is shared, can be a certain process
Protection of the members of the class;
From the memory point of view, we analyze that the memory is logically divided into four segments, where objects are placed in "heap memory"
, the object's reference is placed in the "stack Memory", and the static member is placed in the "Initialize static segment", the first time the class
When loaded, it can be shared by each object in the heap memory, such as;

The static variables of a class, very similar to global variables, can be shared by instances of all classes, as well as static methods of classes
, similar to a global function.
Code Snippets

<?  Class person{//The following is a static member property of a person public static $myCountry = "China";//var $name;////This is a static member method of a person public static function say () { echo "I am Chinese <br>"; }}//Output static property echo Person:: $myCountry; Access static method Person::say (); Assign a value to a static property person:: $myCountry = "United States"; echo Person:: $myCountry;?>

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, static members are not present within each object, but each object can be shared, so if we use the object to access the members, there will be no such attribute definition, the use of object access to static members, in other object-oriented language, such as Java Is that you can access static members using objects, and if you can use objects to access static members in PHP, we try not to use them, because static members we use to access the class name when we do the project. 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 This reference pointer is the object that calls this method. We say that the static method is not called by the object, but the use of the name to access, so there is no object exists, there is no $this this reference, without $this this reference can not access the non-static members of the class, and because the static members of the class can be accessed without objects, So the static method inside the class can only access static properties of the class, that is, $this does not exist, and in static methods we use a special class "Self", and self is similar to $this, but self is the class that represents the static method. In a static method, you can use the class name of the class in which the method resides, or you can use "self" to access other static members, and if there is no special case, we usually use the latter, the "Self:: member property".
Code Snippets

<?  Class person{//The following is a static member property of a person public static $myCountry = "China";//This is a static member method of a person that accesses other static members public static function say () {echo "I am." Self:: $myCountry. " <br> "; }}//Access static method Person::say ();?>

It is also possible to access static members in a non-static method, but you cannot use the "$this" reference to use the class name or the "Self:: member property" form.
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 # define in C if the program changes
Its value, then an error occurs, and the member property that is decorated with "const" is accessed in much the same way as the members of the "static" modifier, 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.
Code Snippets

<?php class myclass{//defines a constant constant const constant = ' constant value '; function Showconstant () {echo self::constant . "\ n"; Use self to access, do not add "$"} to echo Myclass::constant. "\ n"; Use the class name to access, and do not add "$" $class = new MyClass (); $class->showconstant (); echo $class:: constant; It's not allowed.

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.