PHP:我可以使用可变数量的参数声明一个抽象函数吗?

时间:2021-04-03 23:19:08

I want to be able to declare an abstract function in an parent class, with an unknown number of arguments:

我希望能够在父类中声明一个具有未知数量参数的抽象函数:

abstract function doStuff(...);

and then define an implementation with a set of hinted arguments:

然后使用一组提示参数定义一个实现:

 /**
 * @param int $userID
 * @param int $serviceproviderID
 */
static function doStuff($userID, $serviceproviderID) {}

The best approach I've got so far is this,

到目前为止,我得到的最佳方法是,

abstract function doStuff();

 /**
 * @param int $userID
 * @param int $serviceproviderID
 */
static function doStuff() {
    $args = func_get_args();
    ...
}

But every time the function is called, I get a bunch of 'missing argument' warnings because of the hints. Is there a better way?

但每次调用函数时,由于提示,我都会收到一堆“缺少参数”的警告。有没有更好的办法?

Edit: The question's wrong, please don't waste your time answering. The following is what I was looking for, and it seems to work without warnings.

编辑:问题是错的,请不要浪费你的时间回答。以下是我正在寻找的东西,似乎没有任何警告。

abstract class Parent {
    abstract function doStuff();
}

/**
* @param type $arg1
* @param type $arg2
*/
class Child extends Parent {
    function doStuff($arg1, $arg2) {
        ...
    }
}

2 个解决方案

#1


6  

According the comment

根据评论

I specifically want multiple, named arguments as it makes the code more readable.

我特别想要多个命名参数,因为它使代码更具可读性。

abstract public function foo ($a, $b=null, $c=null);

If you want to pass an arbitrary number of values, use arrays

如果要传递任意数量的值,请使用数组

abstract public function foo ($args);

You should avoid "unknown number of arguments", because it makes things more difficult, then necessary: method signatures in interfaces as well as abstract methods should give the user a hint, how the method will work with any implementation. Its an important part, that the user shouldn't need to know anything about the implementation details. But when the number of arguments changes with every implementation, he must know, how the concrete methods are implemented.

你应该避免“未知数量的参数”,因为它使事情变得更加困难,然后是必要的:接口中的方法签名以及抽象方法应该给用户一个提示,该方法如何与任何实现一起工作。它是一个重要的部分,用户不需要知道有关实现细节的任何信息。但是当参数的数量随着每个实现而变化时,他必须知道具体方法是如何实现的。

#2


5  

In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments.

在PHP 5.6及更高版本中,参数列表可能包含...标记,表示该函数接受可变数量的参数。

You can apply this to an abstract class as follows:

您可以将此应用于抽象类,如下所示:

abstract class AbstractExample {
    public function do_something(...$numbers);
}

The arguments will be passed into the given variable as an array.

参数将作为数组传递给给定变量。

#1


6  

According the comment

根据评论

I specifically want multiple, named arguments as it makes the code more readable.

我特别想要多个命名参数,因为它使代码更具可读性。

abstract public function foo ($a, $b=null, $c=null);

If you want to pass an arbitrary number of values, use arrays

如果要传递任意数量的值,请使用数组

abstract public function foo ($args);

You should avoid "unknown number of arguments", because it makes things more difficult, then necessary: method signatures in interfaces as well as abstract methods should give the user a hint, how the method will work with any implementation. Its an important part, that the user shouldn't need to know anything about the implementation details. But when the number of arguments changes with every implementation, he must know, how the concrete methods are implemented.

你应该避免“未知数量的参数”,因为它使事情变得更加困难,然后是必要的:接口中的方法签名以及抽象方法应该给用户一个提示,该方法如何与任何实现一起工作。它是一个重要的部分,用户不需要知道有关实现细节的任何信息。但是当参数的数量随着每个实现而变化时,他必须知道具体方法是如何实现的。

#2


5  

In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments.

在PHP 5.6及更高版本中,参数列表可能包含...标记,表示该函数接受可变数量的参数。

You can apply this to an abstract class as follows:

您可以将此应用于抽象类,如下所示:

abstract class AbstractExample {
    public function do_something(...$numbers);
}

The arguments will be passed into the given variable as an array.

参数将作为数组传递给给定变量。