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

php interface constructor
php interface constants
php interface type hinting
php interface properties
php implement multiple interfaces
php interface vs abstract
php interface static method
php invokable interface

I'm developing a service that is being injected a Logger object but I can have 2 different kind of loggers, I'm planning on having a syslog logger and a queue message system logger. Is this possible?

The idea is having an interface:

interface Loggable
{
    public function log() ;
}

and 2 classes that implement that interface:

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

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

The only way I could come with is having an array as a parameter and use it on one class and not using on the other one... but that is a little bit smelly :P

As stated in the comments, that's not the same interface. If you cannot generalize the interface across all possible logger 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() {
        ...
    }

}

PHP interface optional parameter, Note that it is possible to declare a constructor in an interface, which can be useful More significantly, default argument values may be supplied for interface method parameters, implement the interface in a variety of different ways (e.g. for Prior to PHP 5.3.9, a class could not implement two interfaces that specified a method with the same name, since it would cause ambiguity. More recent versions of PHP allow this as long as the duplicate methods have the same signature. Note: Interfaces can be extended like classes using the extends operator. Note:

You're asking if it's possible: yes it is, but…

If you implement an interface, you must respect its contract.

interface Loggable
{
    public function log();
}

This interface's contract is you can call log() without any parameter.

In order to respect that, you can make the parameter optional:

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

This is perfectly valid PHP and it respects the Liskov Substitution Principle. Of course, you must not use that optional parameter when coding against the interface, else you are obviously breaking the interface. Such parameter can be useful only when you are using the implementation (e.g. in some part of the code which is tightly coupled to the QMSLogger).

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

Object Interfaces - Manual, Interfaces in object-oriented languages like Java, C# and PHP are useful language That's because we can write a method signature that is different from that found in the interface but still satisfies the How is that possible? Minimum PHP version; Class/interface name: The parameter must be an instanceof the given class or interface name. PHP 5.0.0: self: The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0: array: The parameter must be an array.

I came across a similar case where I wanted to create an interface that simply ensure any classes that implemented it would have a method of the same name, but would allow for implementation with different parameters.

/**
 * Interface Loggable
 * @method log
 */
interface Loggable
{

}

Now the Loggable interface can be implemented with different parameters like so.

class Syslogger implements Loggable
{
    public function log($key, $value)
    {
        ...
    }
}

PHP interfaces and optional parameters, By type-hinting the interface in the function parameter list, we can be then you' ve heard that interfaces can define signatures for methods. There are loads of different libraries that do configuration in many ways, or you� PHP Fatal error: Class Dayle contains 1 abstract method and must, therefore, be declared abstract or implement the remaining methods An abstract method is what we call a method without a body. It's what you normally find in interfaces.

You can also pass the parameters as an array , in this way you respect the contract from one hand and also be flexible to insert any values with any amount inside the array , check this out :

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

And to actually use this method you could send the parameters like this :

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

And then inside the concrete implementation you get the parameters like this :

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

Good luck :)

PHP Interfaces Explained, It tells PHP what types of arguments to accept for the function call to be valid. You can define what the requirements for your method are, and make You'll always want to have your errors be as close as possible (in source In our example above, we combine this with the use of an interface as well. Creating more than one method or a function that has a same name but different signatures or parameters in the same class is called method overloading.

Type Declarations using Interfaces in PHP, The classes that implement the interfaces must define all the methods that they inherit from the interfaces, including all the parameters. Can we implement more than one interface in the same class? In order to demonstrate multiple inheritance from different interfaces, we create another interface, Vehicle, that commits� No, its an error If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4.2) methods with same signature is not allowed in this case.

Interface in object oriented PHP, Difference between Interfaces and Abstract classes in PHP. Before getting into theory, let's refresh in memory how an interface is defined: <?php interface InterfaceName { public function method($parameter); }. An interface� Abstract methods inside an abstract class don't have a body, only a name and parameters inside parentheses. In the example given below, we create a public abstract method, calcNumMilesOnFullTank(), that is the skeleton for methods that we will create in the child classes. Once created, these methods will return the number of miles a car can be

PHP: Interface and Abstract Class � @zhukserega, The interface contains no data variables. Interfaces are different from classes as the class can inherit from one class only whereas the class can implement one or It must implement all the methods defined in an interface. A method that takes a parameter array in its signature can be provided with an arbitrary number of parameters. The parameters are put into an array. The type of the array elements determines the parameter types that can be passed to the function. If you define the parameter array with System.Object as the element type, then client code can pass values of any type. In F#, parameter arrays can only be defined in methods.

Comments
  • If the method accepts a parameter in one case and not in another, it's not the same interface!
  • Yes I suppose this is the more correct way but I wanted to be able to inject either class to be able to change the logging system... I guess I was wrong
  • Well that's actually possible in case $queueName defaults to NULL (public function log($queueName = null) {...}) but I agree with deceze here.
  • This is actually the best answer (IMHO). This type of pattern is known as duck typing. It's ideally used for "auto-wiring" during instantiation routines in IoC containers and service managers. The empty interface simply tells the container to call a specific method after instantiation, while letting the dependency injector (DI) determine the parameters to inject via type hinting. Technically, the empty interface is optional, but it's good practice to help prevent the DI container from calling like-named methods in third-party libraries.