I know what the definition is of a Final class, but I want lớn know how and when final is really needed.

Bạn đang xem: Explain final class and final method in php

If I understand it correctly, "final" enables it to extend "Foo".

Can anyone explain when và why "final" should be used? In other words, is there any reason why a class should not be extended?

If for example class "Bar" & class "Foo" are missing some functionality, it would be nice lớn create a class which extends "Bar".



There is a nice article about "When lớn declare classes final". A few quotes from it:

TL;DR: Make your classes always final, if they implement an interface, và no other public methods are defined

Why do I have khổng lồ use final?

Preventing massive inheritance chain of doom Encouraging composition Force the developer to think about user public API Force the developer to shrink an object"s public API A final class can always be made extensible extends breaks encapsulation You don"t need that flexibility You are không tính tiền to change the code

When to avoid final:

Final classes only work effectively under following assumptions:

There is an abstraction (interface) that the final class implements All of the public API of the final class is part of that interface

If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.

P.S. Thanks khổng lồ
ocramius for great reading!

chia sẻ
answered Nov 18, 2015 at 20:03

Nikita U.Nikita U.
3,27111 gold badge2525 silver badges3636 bronze badges
địa chỉ a bình luận |
For general usage, I would recommend against making a class final. There might be some use cases where it makes sense: if you thiết kế a complex API / framework & want lớn make sure that users of your framework can override only the parts of the functionality that you want them to control it might make sense for you khổng lồ restrict this possibility và make certain base classes final.

e.g. If you have an Integer class, it might make sense to make that final in order lớn keep users of your framework form overriding, say, the add(...) method in your class.

Xem thêm: Web Scraping With Php Web Crawler Libraries Are Available? Web Scraping With Php

edited Dec 2, 2019 at 8:05

Yousha Aleayoub
3,77744 gold badges4747 silver badges5959 bronze badges
answered Nov 22, 2010 at 17:09

54922 silver badges44 bronze badges
add a phản hồi |
The reason are:

Declaring a class as final prevents it from being subclassed—period; it’s the over of the line.

Declaring every method in a class as final allows the creation of subclasses, which have access to lớn the parent class’s methods, but cannot override them. The subclasses can define additional methods of their own.

The final từ khoá controls only the ability lớn override and should not be confused with the private visibility modifier. A private method cannot be accessed by any other class; a final one can.

—— quoted from page 68 of the book PHP Object-Oriented Solutions by David Powers.

For example:

final childClassname extends ParentsClassname // class definition omittedThis covers the whole class, including all its methods & properties. Any attempt to create a child class from childClassname would now result in a fatal error. But,if you need khổng lồ allow the class khổng lồ be subclassed but prevent a particular method from being overridden, the final từ khoá goes in front of the method definition.

class childClassname extends parentClassname protected $numPages; public function __construct($autor, $pages) $this->_autor = $autor; $this->numPages = $pages; final public function PageCount() return $this->numPages; In this example, none of them will be able to lớn overridden the PageCount() method.