带有未知参数的PHP扩展

带有未知参数的PHP扩展,php,oop,function,abstract-class,Php,Oop,Function,Abstract Class,我有一个摘要课程和孩子的: abstract class Cubique_Helper_Abstract { abstract public function execute(); } class Cubique_Helper_Doctype extends Cubique_Helper_Abstract{ public function execute($type) {} } 如您所见,方法execute()很常见。但在所有类中,参数的数量可能不同。如何使用方法的不同参数保

我有一个
摘要
课程和孩子的:

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

class Cubique_Helper_Doctype extends Cubique_Helper_Abstract{
    public function execute($type) {}
}
如您所见,方法
execute()
很常见。但在所有类中,参数的数量可能不同。如何使用方法的不同参数保持此扩展

这是我当前的错误:

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

谢谢您,我提前通知。

您可以使用,以便子类的方法签名相同。

您可以使用,以便子类的方法签名相同。

当参数的数量不同时,您会收到错误:

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

因此,您唯一的选择是将参数设置为数组,并在其中传递实际参数,或者在没有参数的情况下声明
execute
,并使用或和

复制发送的参数。当参数的数量不同时,您会收到错误:

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

因此,您唯一的选择是将参数设置为数组,并在其中传递实际参数,或者在不带参数的情况下声明
execute
,并使用or和

对发送的参数进行编译。您可以在不带限制参数的情况下创建方法或函数

function test(){
$num = func_num_args();
for ($i = 0;$i < $num;$i++)
{
    $arg[$i] = func_get_arg($i); 
}
    // process on arguments  
}
功能测试(){
$num=func_num_args();
对于($i=0;$i<$num;$i++)
{
$arg[$i]=func_get_arg($i);
}
//辩论程序
}

您可以在无限制参数的情况下创建方法或函数

function test(){
$num = func_num_args();
for ($i = 0;$i < $num;$i++)
{
    $arg[$i] = func_get_arg($i); 
}
    // process on arguments  
}
功能测试(){
$num=func_num_args();
对于($i=0;$i<$num;$i++)
{
$arg[$i]=func_get_arg($i);
}
//辩论程序
}
  • 您可以从摘要中删除
    execute()
    ,但您可能不想这样做

  • 您也可以给它一个数据对象作为参数,如下所示:

    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();
      }
    }
    
  • 或者,您可以使其依赖于构造函数中的值,并省略参数:

    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
      }
    }
    
最后一个选择是我最喜欢的。这样,您就可以真正确保具有依赖项,并且在执行()时不必给它任何参数


我将不使用
func\u get\u args()
,因为您失去了对依赖项的跟踪。例如:

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)
  • 您可以从摘要中删除
    execute()
    ,但您可能不想这样做

  • 您也可以给它一个数据对象作为参数,如下所示:

    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();
      }
    }
    
  • 或者,您可以使其依赖于构造函数中的值,并省略参数:

    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
      }
    }
    
最后一个选择是我最喜欢的。这样,您就可以真正确保具有依赖项,并且在执行()时不必给它任何参数


我将不使用
func\u get\u args()
,因为您失去了对依赖项的跟踪。例如:

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)

您可以使用
func\u get\u args
获取参数,而不是在签名中定义它们。或者在抽象类中,可以定义方法以接受一个值,即(关联)数组。在子类中,您只需访问所需的密钥。@stereofrog:我完全同意,+1。为了我的学习目的,你能评论我下面的回答吗?欢迎使用
func\u get\u args
获取参数,而不是在签名中定义它们。或者在抽象类中,可以定义方法以接受一个值,即(关联)数组。在子类中,您只需访问所需的密钥。@stereofrog:我完全同意,+1。为了我的学习目的,你能评论我下面的回答吗?Cheers@stereofrog干杯我同意你关于“public”的说法,但是“abstract”对于暗示类型/与抽象类订立契约很有用。我看到了你关于这个话题的另一个答案,我认为你提出的案例(比如
if($\u GET['bla']='bla'))$class='BlaClass';
对某些人来说是有意义的,但如果你事先测试你的类,你就会知道它们是否有效,并且你仍然可以从摘要中获益。如果你问我,不编译意味着有更糟糕、更难测试的案例:)@stereofrog干杯。我同意你关于“public”的说法,但是“abstract”对于暗示类型/与抽象类订立契约很有用。我看到了你关于这个话题的另一个答案,我认为你提出的案例(比如
if($\u GET['bla']='bla'))$class='BlaClass';
对某些人来说是有意义的,但是如果你事先测试你的类,你就会知道它们是否有效,并且你仍然可以从摘要中获益。如果你问我,不编译意味着有更糟糕、更难测试的情况:)