Is it possible to define a method with different parameters in the PHP interface?

I am developing a service into which a Logger object is entered, but I can have two different loggers, I plan to have a syslog logger and a system queue message logger. Is it possible?

The idea has an interface:

interface Loggable { public function log() ; } 

and 2 classes that implement this interface:

 class Syslogger implements Loggable { public function log() { ... } } class QMSLogger implements Loggable { public function log($queueName) { ... } } 

The only way I could work is to have an array as a parameter and use it on one class, and not use it on another ... but it's a little smelly: P

+7
source share
3 answers

As stated in the comments, this is not the same interface. If you cannot generalize the interface in all possible registrar implementations, make the configuration differences part of the instance constructor:

 class QMSLogger implements Loggable { protected $queueName; public function __construct($queueName) { $this->queueName = $queueName; } public function log() { ... } } 
+8
source

You ask if this is possible: yes, but ...

If you are implementing an interface, you must abide by its contract .

 interface Loggable { public function log(); } 

You can call log() with this interface contract without any parameters .

To do this, you can make the parameter optional:

 class QMSLogger implements Loggable { public function log($queueName = null) { ... } } 

This is a well-functioning PHP , and it follows the Liskov Substitution Principle. Of course, you should not use this optional parameter when coding with an interface , otherwise you are obviously breaking the interface. Such a parameter can be useful only when you use an implementation (for example, in some part of the code that is closely related to QMSLogger ).

However, this is probably not the solution to your problem, since $queueName seems to be a configuration value, and it might be better to pass it in the class constructor (as explained in another answer).

+8
source

You can also pass parameters in the form of an array, so you respect the contract on the one hand, and also have to be flexible to insert any values ​​with any amount inside the array, check this:

 abstract class FeaturesAbstract { /** * @param array $paramsArray * * @return mixed */ abstract public function addExecute($paramsArray); } 

And to use this method, you can send the following parameters:

 $this->abstract->addExecute(array('paramA' => $paramA, 'paramB' => $paramB)); 

And then inside the concrete implementation you will get the following parameters:

  /** * @param array $paramsArray * * @return void */ public function addExecute($paramsArray) { $a = $paramsArray['paramA']; $b = $paramsArray['paramB']; $c = ... } 

Good luck :)

+1
source

All Articles