PHP Object-oriented final keyword usage and examples

Source: Internet
Author: User
For this lesson, let's take a look at the concept and usage of the final keyword.

What is the final keyword?

Final Chinese translates as "ultimate", "final". The final keyword modification before declaring a class means that the function will not be overloaded in any subclass, inheriting, that is, the final decorated class will no longer have subclasses.

Prior to the course we said 4 kinds of keywords, public, private, protected, static, respectively, is common, private, protected, static. Usage and examples are available in the previous course, and students who want to learn can refer to the previous course:

PHP Object-oriented static method use

PHP Object-oriented data hiding

Below we use examples to compare the final with other keywords what is different, and then to analyze the final usage:

Final usage examples are detailed:

<?phpheader ("Content-type:text/html;charset=utf-8"); class a{                                               //DECLARE class public $num = "all"; final function operation () {                            //create method, with final keyword echo ' above variable $num number is '. $this->num;}} Class B extends a{                                    //Create subclass B Inherits Class A Public $num = "", function operation () {echo ' This variable $num number is '. $this->num;}} $Num = new B ();                                         Instantiate subclass $num->operation ();                                     Calling a method in a subclass

We first created a Class A, adding a method to Class A, but the keyword before the method we used final, that is, the method operation () in Class A will no longer be inherited, overloaded. Then create Class B to inherit Class A, including the method operation () that cannot be inherited, and the result is only error.

In the example above, the operation () method in Class B is disabled for overloading. This will only make an error.

If final is written before the class, then the entire class is forbidden to inherit. Like the example above:

Final Class a{}

This way, the whole class A cannot be inherited or overloaded.

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.