Hi Guys,

I have a interface and class as follows:

interface Math
{
    public function calculate($value);
}

class Tax implements Math
{
    public function calculate($value, $rate)
    {
        return ($value * (($rate / 100) + 1));
    }
}

So my problem is that the calculate() method in the class has more parameters than that allowed by the interface.

How can I specify that the calculate() method can have multiple parameters, but must have the first $value parameter?

Any ideas?

    This seems to work:

    <?php
    interface Test
    {
       public function foo($bar);
    }
    
    class MyClass implements Test
    {
       public function foo($bar)
       {
          if(func_num_args() != 2) {
             throw new Exception("Missing arg");
          }
          $arg2 = func_get_arg(1);
          return $bar + $arg2;
       }
    }
    
    $mc = new MyClass();
    echo $mc->foo(3, 5); // 8
    ?>
    

      I would like to add, though, that to my mind this sort of breaks the "contract" that a interface implies: which is that any class that implements it will have those methods provide the same interface to the calling code, regardless of which implementing class is being used.

        Write a Reply...