final keyword

WBOY
Release: 2016-07-29 08:51:13
Original
1520 people have browsed it

The Chinese meaning of final is final and final. Classes and methods modified by the keyword final are the "final classes and methods". In other words, classes modified with the final keyword cannot be inherited, and methods modified with the final keyword cannot be overridden.

If there is a class in the format:

final class MyClass{

//...

}

, it means that other classes can no longer use the extends keyword to inherit this class.

If there is a method modified with the final keyword, as shown below:

final function method_name()

It means that this method cannot be overridden in subclasses of the class where it is located.

For example:

Generate a subclass ChildClass for the final class MyClass. You can see that the program reports an error and cannot be executed. The code is as follows:


<?php

final class MyClass
{
    function __construct()
    {
        echo "111111";
    }
}

class ChildClass extends MyClass
{
    static function test()
    {
        echo "22222";
    }
}

$child = new ChildClass();
Copy after login

The result is:

Fatal error: Class ChildClass may not inherit from final class (MyClass) in D:wampserverwwwtesttestindex1.php on line 17

The above has introduced the final keyword, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template