Home Backend Development PHP Tutorial Application of php final keyword

Application of php final keyword

Oct 14, 2019 pm 03:25 PM
final

PHP 5 adds a new final keyword. If a method in the parent class is declared final, the child class cannot override the method. If a class is declared final, it cannot be inherited.

This keyword can only be used to define classes and methods. The final keyword cannot be used to define member properties, because final means constant. We use the define() function to define constants in PHP. , so final cannot be used to define member properties.

Classes marked with the final key cannot be inherited;

<?php
final class Person
{
    function say()
    {
    }
}
 
class Student extends Person
{
    function say() 
    {
    }
}
?>
Copy after login

The following error will occur:

Fatal error: Class Student may not inherit from final class (Person)

Methods using the final key mark cannot be overridden by subclasses and are the final version;

<?php
class Person
{
    final function say() 
    {
    }
 
}
class Student extends Person
{
    function say() 
    {
    }
}
?>
Copy after login

The following error will occur:

Fatal error: Cannot override final method Person::say()

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Application of php final keyword. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between final, finally, and finalize in Java The difference between final, finally, and finalize in Java Feb 19, 2024 pm 12:16 PM

The difference between final, finally, and finalize in Java requires specific code examples. In Java programming, you often encounter the three keywords final, finally, and finalize. Although they are spelled similarly, they have different meanings and usages. This article will explain the differences between these three keywords in detail and give code examples to help readers better understand. 1. Final keyword The final keyword can be used for classes, methods and variables. Its function is to make the modified class

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

What is the function of java final keyword What is the function of java final keyword Nov 25, 2022 pm 04:26 PM

In Java, final can be used to modify classes, methods and variables. The final modified class means that the class cannot be inherited by any other class, which means that this class is a leaf class in an inheritance tree, and the design of this class has been considered perfect and does not need to be modified or extended. The method in the final modified class means that the class cannot be inherited by any other class and cannot be overridden; that is, the method is locked to prevent the inherited class from changing it. final modifies a variable in a class, indicating that the variable cannot be changed once it is initialized.

How are final objects created in Java? How are final objects created in Java? Apr 11, 2024 pm 02:00 PM

There are two ways to create final objects in Java: declaring a final variable or declaring a class using the final modifier. When a final variable is declared, the object is created through an initializer; when a final class is declared, the class instance is immutable. Importantly, references to final objects can still change, but the objects they point to are immutable.

final variable in java final variable in java Sep 24, 2023 pm 08:49 PM

Final variables can only be explicitly initialized once. A reference variable declared as Final can never be reassigned to refer to a different object. However, the data within the object can be changed. Therefore, the state of the object can change, but the reference cannot. For variables, the final modifier is usually used with static to make the constant a class variable. Example publicclassTest{ finalintvalue=10; //Thefollowingareexamplesofdeclaringconstants: &a

Special syntax in PHP: Static, Final, Abstract and other keywords Special syntax in PHP: Static, Final, Abstract and other keywords May 11, 2023 pm 04:00 PM

PHP is a popular open source server-side scripting language widely used in web development. The PHP language is not only easy to learn and use, but also supports a variety of programming paradigms, object-oriented programming, functional programming, etc. In PHP, there are some special syntax keywords, such as Static, Final, Abstract, etc. These keywords have special functions in object-oriented programming. This article will introduce these keywords in detail. Static keyword In PHP, the Static keyword has two uses

Why can't constructors be final in Java? Why can't constructors be final in Java? Aug 20, 2023 pm 07:01 PM

Whenever you declare a method as final, you cannot override it. That is, you cannot provide a subclass with an implementation of a superclass's final method. That is, the purpose of declaring a method as final is to prevent modification of the method from outside (subclasses). In inheritance, when you extend a class, the subclass inherits all members of the superclass except the constructor. In other words, constructors cannot be inherited in Java, therefore you cannot override a constructor. Therefore, there is no point in prefixing the constructor with final. Therefore, Java does not allow the use of final keyword before a constructor. If you try to declare a constructor final, a compile-time error will be generated saying "modifierf

Java Error: Abuse of final keyword, how to solve and avoid it Java Error: Abuse of final keyword, how to solve and avoid it Jun 24, 2023 pm 09:00 PM

As a very popular programming language, Java is widely used in the Internet, mobile devices and other fields. For Java developers, they often encounter some errors or problems, one of which is the abuse of the final keyword. In Java, the final keyword is often used to modify variables, methods, classes, etc. It indicates that the attribute cannot be changed after it is defined. The final keyword can help developers ensure the immutability of objects and avoid problems such as race conditions, but misuse of the final keyword

See all articles