Hướng dẫn when to use the final keyword in php? - khi nào sử dụng từ khóa cuối cùng trong php?

Từ khóa cuối cùng ngăn các lớp con ghi đè một phương thức hoặc hằng số bằng cách tiền tố định nghĩa với final. Nếu bản thân lớp đang được xác định cuối cùng thì nó không thể được mở rộng.

Ví dụ #1 Phương pháp cuối cùng ví dụ

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }

      final public function

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>

Ví dụ #2 Ví dụ về lớp cuối cùng

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
}
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>

Ví dụ #3 Hằng số cuối cùng Ví dụ như của Php 8.1.0

<?php
class Foo
{
    final public const 
"foo";
}

class

Bar extends Foo
{
    public const 
"bar";
}
// Fatal error: Bar::X cannot override final constant Foo::X
?>

Lưu ý: Thuộc tính không thể được khai báo cuối cùng: chỉ các lớp, phương thức và hằng số (kể từ Php 8.1.0) có thể được khai báo là cuối cùng. Kể từ Php 8.0.0, các phương thức riêng tư có thể không được khai báo cuối cùng ngoại trừ hàm tạo.: Properties cannot be declared final: only classes, methods, and constants (as of PHP 8.1.0) may be declared as final. As of PHP 8.0.0, private methods may not be declared final except for the constructor.

Penartur tại Yandex Dot Ru ¶

15 năm trước

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
0

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
1

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
2

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
3

Ai đó chấm khác ở nơi khác chấm lưới ¶

8 năm trước

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
4

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
5

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
6

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
7

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
8

Cottton tại I-Stats Dot Net

8 năm trước

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
9

      final public function0

      final public function1

Cottton tại I-Stats Dot Net

8 năm trước

      final public function2

      final public function3

      final public function4

      final public function5

      final public function6

Cottton tại I-Stats Dot Net

Mattsch tại gmail dot com

      final public function7

Santoshjoshi2003 tại Yahoo Dot Co Dot trong ¶

13 năm trước

      final public function8

      final public function5

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
0

Ẩn danh ¶

12 năm trước

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
1

slorenzo tại clug dot org dot ve ¶

13 năm trước

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
2

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
3

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
4

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
5

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
6

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
7

Ẩn danh ¶

8 năm trước

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
8

moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class

ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
9

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
0

Cottton tại I-Stats Dot Net

Mattsch tại gmail dot com

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
1

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
2

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
3

Santoshjoshi2003 tại Yahoo Dot Co Dot trong ¶

13 năm trước

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
4

      final public function5

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
6

Ẩn danh ¶

12 năm trước

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
7

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
8

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
// As the class is already final, the final keyword is redundant
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}
9

class0

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
3

Các tình huống tốt nhất khi chúng ta sử dụng từ khóa cuối cùng là gì?

Trong Java, từ khóa cuối cùng có thể được sử dụng trong khi khai báo một thực thể. Sử dụng từ khóa cuối cùng có nghĩa là giá trị không thể được sửa đổi trong tương lai. Thực thể này có thể - nhưng không giới hạn ở - một biến, một lớp hoặc một phương thức.while declaring an entity. Using the final keyword means that the value can't be modified in the future. This entity can be - but is not limited to - a variable, a class or a method.

Khi nào nên sử dụng cuối cùng?

Bạn sử dụng từ khóa cuối cùng trong khai báo phương thức để chỉ ra rằng phương thức không thể bị ghi đè bởi các lớp con. Lớp đối tượng thực hiện một số phương thức của nó là cuối cùng.to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .

Mục đích của việc sử dụng từ khóa cuối cùng là gì?

Từ khóa cuối cùng là một công cụ sửa đổi không truy cập được sử dụng cho các lớp, thuộc tính và phương thức, điều này làm cho chúng không thể thay đổi (không thể kế thừa hoặc ghi đè).Từ khóa cuối cùng rất hữu ích khi bạn muốn một biến luôn lưu trữ cùng một giá trị, như PI (3.14159 ...).Từ khóa cuối cùng được gọi là "công cụ sửa đổi".a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".

Việc sử dụng từ khóa cuối cùng trong kế thừa là gì?

Cuối cùng là một từ khóa trong Java được sử dụng để hạn chế một số chức năng.Chúng ta có thể khai báo các biến, phương thức và các lớp với từ khóa cuối cùng.Trong quá trình kế thừa, chúng ta phải khai báo các phương thức với từ khóa cuối cùng mà chúng ta được yêu cầu tuân theo cùng một triển khai trong suốt tất cả các lớp dẫn xuất.restricting some functionalities. We can declare variables, methods, and classes with the final keyword. During inheritance, we must declare methods with the final keyword for which we are required to follow the same implementation throughout all the derived classes.