在 PHP 中实例化新类实例时的参数打包和解包
在 Python 中,您可以像这样打包和解包参数:
def myfunction(arg1, arg2, arg3):
pass
mydict = dict(arg1='foo', arg2='bar', arg3='baz')
myfunction(**mydict)
mylist = ['foo', 'bar', 'baz']
myfunction(*mylist)
现在,PHP 中有类似的东西吗?我有一个传递给函数的类,以及从数据库记录中提取的关联数组。我想创建该类的一个实例,它接受与数组中的键命名相同的参数。能做到吗?
编辑
好的,所以我肯定没有正确地表达我的问题。当我事先既不知道类名也不知道参数数量时,我想动态实例化一个新类。
我找到了答案:
http://php.net/manual/en/book .reflection.php
这是解决方案:
class MyClass {
public function __construct($var1, $var2, $var3) {
echo "$var1\n";
echo "$var2\n";
echo "$var3\n";
}
}
$reflector = new ReflectionClass('MyClass');
$args = array('var1'=>'foo', 'var2'=>'baz', 'var3'=>'bar');
$instance = $reflector->newInstanceArgs($args);
In Python, you can pack and unpack arguments like so:
def myfunction(arg1, arg2, arg3):
pass
mydict = dict(arg1='foo', arg2='bar', arg3='baz')
myfunction(**mydict)
mylist = ['foo', 'bar', 'baz']
myfunction(*mylist)
Now, is there something similar in PHP? I have a class that is passed to a function, along with an associative array extracted from a database record. I want to create an instance of the class, which accepts arguments that are named the same as the keys in the array. Can it be done?
EDIT
Ok, so I definitely didn't formulate my question correctly. I wanted to instantiate a new class dynamically when I know neither the class name, nor the number of arguments beforehand.
I've found the Answer though:
http://php.net/manual/en/book.reflection.php
Here's the solution:
class MyClass {
public function __construct($var1, $var2, $var3) {
echo "$var1\n";
echo "$var2\n";
echo "$var3\n";
}
}
$reflector = new ReflectionClass('MyClass');
$args = array('var1'=>'foo', 'var2'=>'baz', 'var3'=>'bar');
$instance = $reflector->newInstanceArgs($args);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,您不能直接执行此操作,您必须手动从数组中提取值并将它们传递给构造函数,或者更改它以使其接受数组作为参数。
您可能会看一下 call_user_func_array 但我不要认为您可以将它与构造函数一起使用,因为您需要对象的实例来调用其方法之一。
No you can't do it directly, you'll have to manually extract the values from the array and pass them to the constructor, or change it so that it accepts an array as argument.
You might take a look at call_user_func_array but I don't think you can use it with a constructor as you need an instance of the object to call one of its methods.
请注意,参数列表解包
myfunc(*args)
可以通过 call_user_func_array():对于对象方法,您可以使用
call_user_func_array(array($obj, "__construct"), $args)
。这里的缺陷是 __constructor 因此会被调用两次,所以不应该做任何有趣的事情。否则,需要特定的 init 方法。由于缺少命名参数,PHP 中不存在与
myfunc(**args)
等价的参数。因此,一个常见的习惯用法是只传递字典/数组,并让函数处理命名的参数。Note that the param list unpacking
myfunc(*args)
can be achieved with call_user_func_array():For object methods you can use
call_user_func_array(array($obj, "__construct"), $args)
. The deficiency here is the __constructor thus would be invoked twice, so should not do any funny things. Otherwise a specific init method is necessary.Due to lack of named parameters an equivalent to
myfunc(**args)
does not exist in PHP. A common idiom is therefore to just pass the dict/array in, and let the function handle named params.