Tirth Bodawala Tirth Bodawala - 9 months ago 44
PHP Question

Is a final static class method possible?

I need a class to inherit another class. I have a static function

in the base class, and I don't want that method to be inherited in the derived class. I tried the
keyword but it's not working. What should I do?

Answer Source

You're misunderstanding what final means.

One of the foundation principles of OOP is that if a class is a subclass of another class, then that class gets all the functionality of the class it inherits from. You can't inherit just some of the superclass functionality, it's all or nothing.

The reason for this is a bit tricky to get to grips with but once you do, you'll realise that it makes sense. Namely, if a piece of code is able to process items of a particular class, then it must also be able to process any subclass of the item as well. If a subclass didn't have all the functionality of its superclass, you couldn't do that.

For example, imagine you have a class that defines interfacing with a payment gateway. As all payment gateways are different, you couldn't (or at least shouldn't) implement a single class that can interface with any of the payment gateways that are available. But in general, payment gateways work in the same basic way. A request is sent, including a user's credit card details and amount you want to debit from the card. The payment gateway responds with a message saying whether payment was authorized or not.

You'd implement that kind of functionality with an abstract superclass that defines how you interface with payment gateways in general, then you'd create a subclass for each payment gateway you want to use (paypal, sagepay, whatever).

As all these subclasses inherit from the same base class, you can be certain that they all implement certain methods, and you can send messages to those methods without worry. As long as the payment class you're dealing with is a subclass of your abstract payment class, then everything should work.

If one of the subclasses was able to not implement something from the abstract superclass, then you couldn't be sure that sending a particular message to a subclass would work, or throw an exception. This would make inheritance rather pointless.

Final in PHP (and indeed most OOP languages that implement it or similar concepts) means "This is the final implementation of this method. If a programmer subclasses me, then he can't replace my final methods with his own implementation". It doesn't mean that the methods will not be available in the subclass, just that you can't change them.

If you really must remove functionality from a subclass, you can do it by overriding the superclass method and replacing it with an empty method (one that does nothing). But don't do that, as it will cause problems of the type discussed above.

Generally, if you run into situations where you have functionality in a superclass that you don't need in a subclass, it's a code smell that's telling you that you might need to rethink your design.