1

I want to have this interface:

interface Resource_Controller_Interface {
    public function index_get():stdClass;
    public function index_get(mixed $key):array;
    public function index_post():bool;
    public function index_put(mixed $key):bool;
    public function index_patch(mixed $key):bool;
    public function index_delete(mixed $key):bool;
}

Unfortunately, I cannot do two methods called 'index_get', even with different signatures.

I really want this interface to have these names though, is there a way I can do it? (using $key = null will not force implementation...)

2
  • 1
    possible duplicate of stackoverflow.com/questions/4697705/php-function-overloading Commented Jun 23, 2016 at 20:29
  • in general, cases like this are best handled by using both an interface and an abstract class, then you can have slightly modified version of the name in the interface ( to force implementation ) and then in the abstract class have a third function that sort of brings those together, if that makes sense. Just a thought. Commented Jun 23, 2016 at 21:13

1 Answer 1

1

You can make use of the func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

This way the function name will be same but you will be able to do different things using some conditionals.

Sign up to request clarification or add additional context in comments.

3 Comments

I can just do null in this case, but why isn't the different signature allowed?
Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name.
dont forget function_get_args ( with an s )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.