开发者

PHP extending with unknown arguments

I have an abstract class and child's:

abstract class Cubique_Helper_Abstract {
    abstract public function execute();
}

class Cubique_Helper_Doctype extends Cubique_Helper_Abstract{
    public function execute($type) {}
}

As you can see, method execute() is common. But number of ar开发者_如何学JAVAguments can be different in all classes. How can I keep this extending with different arguments of method?

It's my current error:

Declaration of Cubique_Helper_Doctype::execute() must be compatible
with that of Cubique_Helper_Abstract::execute()

Thank you i advance.


you can make method or function with out limited argument.

function test(){
$num = func_num_args();
for ($i = 0;$i < $num;$i++)
{
    $arg[$i] = func_get_arg($i); 
}
    // process on arguments  
}


  • You could remove execute() from the abstract, but you probably do not want to do that.

  • You could also give it an data object as argument, like this:

    class MyOptions {
      public function getType(){}
      public function getStuff(){}
    }
    
    abstract class Cubique_Helper_Abstract {
        abstract public function execute(MyOptions $options);
    }
    
    class Cubique_Helper_Doctype extends Cubique_Helper_Abstract{
      public function execute(MyOptions $options) {
        $type = $options->getType();
      }
    }
    
  • Or, you could make it depend on values in constructor and leave out the argument:

    abstract class Cubique_Helper_Abstract {
        abstract public function execute();
    }
    
    class Cubique_Helper_Doctype extends Cubique_Helper_Abstract{
      public function __construct($type) {
        // since __construct() is not in abstract, it can be different
        // from every child class, which let's you handle dependencies
        $this->type = $type;
      }
      public function execute() {
        // you have $this->type here
      }
    }
    

The last alternative is my favorite. This way, you really make sure you have the dependencies and when it's time to execute() you don't have to give it any arguments.


I would not use func_get_args() since you lose track of dependencies. Example:

class Cubique_Helper_Doctype extends Cubique_Helper_Abstract {
  public function execute() {
    $args = func_get_args();
    $type = $args[0];
    $title = $args[1];
    $content = $args[2];
    // do something, for example
    echo $type; // will always echo "" if you miss arguments, but you really want a fatal error
  }
}

$d = new Cubique_Helper_Doctype();
$d->execute();
// this is a valid call in php, but it ruins your application.
// you **want** it to fail so that you know what's wrong (missing dependencies)


You could use func_get_arg so that the child class's method signature is the same.


When the number of arguments is different, you receive the error:

Fatal error: Declaration of Cubique_Helper_Doctype::execute() must be compatible with that of Cubique_Helper_Abstract::execute() in C:\wamp\www\tests\41.php on line 16

So your only option is to make the arguement an array and pass in it the actual arguments or make the declaration of execute with no parameters and mamipulate the sent arguments with func_get_args or func_get_arg and func_num_args

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜