Deep analysis of PHPfinal keywords tips _ PHP Tutorial

Source: Internet
Author: User
In-depth analysis of PHPfinal keyword usage skills. PHP programmers have mastered that class inheritance in PHP is the most commonly used programming feature. we can create a base class (parent class) and then define some basic methods in it, in my PHP programmers

In PHP, class inheritance is the most commonly used programming feature. we can create a base class (parent class) and then define some basic methods in it, in our subclass, we can extend the methods in the parent class. this is called overwrite. However, if you have some key methods in the parent class, you cannot inherit from the quilt class, if the subclass inherits this method, it may cause some trouble for the program. Therefore, we hope this method is "private" and cannot be extended, in PHP, the final keyword is used to modify the methods you want to inherit.

PHP final keywords can modify classes and methods in classes, but they have similar functions, that is, if you use final keywords to modify them, the modified class or method cannot be extended or inherited. You can only honestly reference it. If you use final before the class, this indicates that this class cannot use inheritance. if you use the PHP final keyword before the method, this indicates that this method cannot be overwritten. The truth is that simple. let's take a look at a simple example.

 
 
  1. < ?PHP
  2. final class BaseClass {
  3. public function test() {
  4. echo "BaseClass::test() calledn";
  5. }
  6. final public function moreTesting() {
  7. echo "BaseClass::moreTesting() calledn";
  8. }
  9. }
  10. class ChildClass extends BaseClass {
  11. public function moreTesting() {
  12. echo "ChildClass::moreTesting() calledn";
  13. }
  14. }
  15. // Results in Fatal error: Cannot override final method BaseClass::moreTesting()
  16. ?>

In this example, we added the PHP final keyword before the class and the method to modify our specific class. of course, this class cannot be output as required. you can debug it by yourself, this is the basic principle. we only need to remember that if the class or method uses final, this means that the class cannot be inherited or the subclass cannot override the method of the parent class.


Class inheritance in PHP is the most used programming feature. we can create a base class (parent class) and then define some basic methods in this class. in my...

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.