2

Suppose I have several classes with a method

public function foo (int $a, int $b, ..) {..}

where the number of parameters depends on the particular class (but is fixed for a specific class).

Is there a way to put such a method in an interface so that all my classes could implement that interface?

I don't think variadics can help here, because I would need to define all my foo implementations as

public function foo(int ...$ints) {..}

losing the information around how many parameters I need to have for a specific class.

Any trickery that could be used?

P.S. I would really like to avoid exceptions, so I can't just check if the number of arguments matches programmatically

4
  • Pass an array of params instead of individual params. Commented Sep 30, 2017 at 8:50
  • @BSB that's what you have using variadics. Bit you lose the information of how many parameters are needed by every single class Commented Sep 30, 2017 at 8:54
  • Couldn't understand fully. But if you want number of parameters passed, You can count params array Indexes. Commented Sep 30, 2017 at 8:59
  • I could do that, but what if the number is wrong? I would like to force the client of my class to pass the correct number of arguments Commented Sep 30, 2017 at 9:10

2 Answers 2

2

There is no way of achieving that without using something like func_num_args or count($ints) and throwing an exception on failure.

This also seems like a potential design flaw.

If you really need an interface and your parameter is a set of values, I would really recommend receiving an array and checking number and type of items in that array.

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

1 Comment

I guess an Interface is too restrictive for my purpose. Still, I don't think at this point that there is a way to communicate that my classes have a too method receiving a certain number of integers
2

If parameters differ, while the method's name is the same, then it is not the same interface. It's simple as that.

You are trying to put a round peg in a square hole.

3 Comments

yeah, an interface is too much here. What I could do is having an empty interface, just to name things. Or many interfaces, one per number of arguments. Still, these are not nice solutions
Instead of having each parameter separate, you could pass in an instance instead.
No idea why the downvote here, this is perfectly correct. An interface is to make a contract with some specific methods and their params and return type. If the params are different, then you are creating objects with different values, which makes them different objects. if UserClassA can have property "name" as string or null, and UserClassB property "name" cannot be null, they are not to be interfaced objects.

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.