Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface with not-required methods

Tags:

php

interface

Is there a way to indicate optional method in the interface (so that the contract only indicated the number / type of arguments to be given)?

Please give maybe a little more understanding and insight into the problem, and indicate a solution? See for instance this discussion: Optional Methods in Java Interface

In the app I'm using Listeners connected to the Persistence (Doctrine). So I'm using some of these methods:

prePersist()
preUpdate()
postPersist()
postUpdate()

etc.

Now, while refactoring, since there are too many Entities (objects to be persisted) I decided to split the parts of these methods into separate classes.

However not all of them need all pre-... and post-... methods. I need to make sure they are given appropriate number and type of arguments. How do you do that in PHP?

like image 860
forsberg Avatar asked Oct 19 '25 04:10

forsberg


1 Answers

No. The whole idea of interfaces is to have a contract that guarantees that a method exists.

But a class can implement multiple interfaces, so you can define a different interface that contains that method and not add that interface to the class that doesn't have the method.

like image 54
GolezTrol Avatar answered Oct 21 '25 16:10

GolezTrol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!