PHP 中的可变函数和类型提示 [英] Variadic functions and type-hinting in PHP

查看:22
本文介绍了PHP 中的可变函数和类型提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速一:

有没有办法在 PHP 中强制执行可变参数函数的类型?我假设没有,但也许我错过了一些东西.

Is there any way to enforce types for variadic functions in PHP? I'm assuming not, however maybe I've missed something.

截至目前,我只是强制使用所需类型的单个必需参数,并迭代检查其余参数.

As of now, I'm just forcing a single required argument of the needed type, and iterating to check the rest.

public function myFunction(MyClass $object){
    foreach(func_get_args() as $object){
        if(!($object instanceof MyClass)){
            // throw exception or something
        }
        $this->_objects[] = $object;
    }
}

有更好的解决方案吗?

目的:

一个容器对象,它充当子对象的迭代列表,具有一些实用功能.使用可变参数构造函数调用它会是这样的:

A container object that acts as an iterated list of the child objects, with some utility functions. calling it with a variadic constructor would be something like this:

// returned directly from include
return new MyParent(
    new MyChild($params),
    new MyChild($params),
    new MyChild($params)
);

另一个选项可以是 addChild 方法链:

The other option could be an addChild method chain:

$parent = new MyParent;
return $parent
    ->addChild(new MyChild($params))
    ->addChild(new MyChild($params))
    ->addChild(new MyChild($params));

孩子们也将几个参数传递给他们的构造函数,所以我试图在易读性和处理费用之间取得平衡.

The children take several arguments to their constructor as well, so I'm trying to balance between legibility and processing expense.

推荐答案

嗯,我会说这取决于参数的数量 :) 没有什么像列表(所有参数 1-n 作为 MyClass [在 PHP 5.6 之前,对于PHP 5.6+ 参见可变参数]),更多的是您需要编写每个参数(如您的问题),并且允许发送更多参数,但只会检查定义的参数.

Well I would say it depends on the number of arguments :) There is nothing like a list (all arguments 1-n as MyClass [before PHP 5.6, for PHP 5.6+ see Variadic functions]), it's more that you need to write each argument (as in your question) and it's allowed to send more but only the ones defined will be checked.

但是,您可以定义更多并使其可选.因此,为什么我只写它取决于参数的数量:

However you can define more and make them optional. Hence why I just wrote it depends on the number of arguments:

public function myFunction(MyClass $object, MyClass $object2=null, MyClass $object3=null, MyClass $object4=null, ...){
    foreach(func_get_args() as $object){
        if(null === $object){
            // throw exception or something
        }
        $this->_objects[] = $object;
    }
}

在这样的例子中,当参数不是 NULL 也不是 MyClass 时,PHP 会抛出异常(如果作为默认值,则传递 NULL,也可以通过).可选参数不应是 func_get_args() 返回值的一部分.我的意思是,如果你不传递第三个参数(这里命名为 $object3),它就不会出现在 func_get_args().

With such an example, PHP would have thrown the exception already when the argument is not NULL and not MyClass (Passing NULL if given as default value, is possible to pass as well). Optional parameter should not be part of the return value of func_get_args(). I mean, if you don't pass the third argument (here named $object3) it won't appear in the array returned by func_get_args().

不知道这是否比问题中的代码更适合您.

No idea if that is more practicable for you than your the code in the question.

如果您在代码中遇到更多此类情况,您可以创建某种帮助程序来验证函数调用的输入并委托以从帮助程序中抛出带有良好错误消息的异常.这将防止重复代码并在开发过程中提供更多便利,因为您可以在为多个案例编写异常通知时使它们更好.

If you face more such situations in your code, you can create some sort of helper to validate the input of function calls and delegate to throw the exceptions with nice error message from the helper. That will prevent duplicate code and will give more comfort in development as you can make the exception notices nicer as you write them for multiple cases.

根据你的新反馈,如果你想让解释器首先让检查工作,稍微迭代和 addMember 函数就可以了:

According to your new feedback, if you want first of all the interpreter let the work of checking, a little iteration and the addMember function would do it:

class MyParent {
    private $children = array();
    public function __construct() {
        foreach(func_get_args() as $object) $this->addChild($object);
    }
    public function addChild(MyChild $object) {
        $this->children[] = $object;
    }
}

在任何列表中使用错误类型的对象实例化对象将阻止实例化集合.

Instantiating the object with a wrong type of object in any of the list will prevent the Collection to be instantiated.

然后你可以这样做:

// returned directly from include
return new MyParent(
    new MyChild($params),
    new MyChild($params),
    new MyChild($params)
);

这篇关于PHP 中的可变函数和类型提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆