call_user_func_array传递参数到构造函数 [英] call_user_func_array passing arguments to a constructor
问题描述
我搜索了很多Google结果页面,以及这里的stackoverflow,但找不到一个似乎适合我的情况的解决方案。我似乎只有一个最后一个陷阱在我试图建立的函数,它使用call_user_func_array动态创建对象。
I have searched many a page of Google results as well as here on stackoverflow but cannot find a solution that seems to fit my situation. I appear to have but one last snag in the function I am trying to build, which uses call_user_func_array to dynamically create objects.
我可以得到的致命错误是类Product的对象无法转换为字符串
。当错误发生时,在日志中我得到其中的五个(每个参数一个): PHP警告:缺少参数1 Product :: __ construct(),
致命错误。
The catchable fatal error I am getting is Object of class Product could not be converted to string
. When the error occurs, in the log I get five of these (one for each argument): PHP Warning: Missing argument 1 for Product::__construct(),
before the catchable fatal error.
这是函数的代码:
public static function SelectAll($class, $table, $sort_field, $sort_order = "ASC")
{
/* First, the function performs a MySQL query using the provided arguments. */
$query = "SELECT * FROM " .$table. " ORDER BY " .$sort_field. " " .$sort_order;
$result = mysql_query($query);
/* Next, the function dynamically gathers the appropriate number and names of properties. */
$num_fields = mysql_num_fields($result);
for($i=0; $i < ($num_fields); $i++)
{
$fetch = mysql_fetch_field($result, $i);
$properties[$i] = $fetch->name;
}
/* Finally, the function produces and returns an array of constructed objects.*/
while($row = mysql_fetch_assoc($result))
{
for($i=0; $i < ($num_fields); $i++)
{
$args[$i] = $row[$properties[$i]];
}
$array[] = call_user_func_array (new $class, $args);
}
return $array;
}
现在,如果我注释掉call_user_func_array行并将其替换为: / p>
Now, if I comment out the call_user_func_array line and replace it with this:
$array[] = new $class($args[0],$args[1],$args[2],$args[3],$args[4]);
页面按原样加载,并填充我正在构建的表。所以一切都是绝对功能,直到我尝试实际使用 $ args
数组 call_user_func_array
。
The page loads as it should, and populates the table I am building. So everything is absolutely functional until I try to actually use my $args
array within call_user_func_array
.
有没有一些关于调用该数组的细节?我读了一次call_user_func_array的PHP手册,然后一些,在那个页面的例子似乎显示人们只是建立一个数组,并调用它的第二个参数。
Is there some subtle detail about calling that array that I am missing? I read the PHP manual for call_user_func_array once, and then some, and examples on that page seemed to show people just building an array and calling it for the second argument. What could I be doing wrong?
推荐答案
您不能调用 $ class
You can't call the constructor of $class
like this:
call_user_func_array (new $class, $args);
这不是有效回电作为第一个参数。让我们分开选择:
That's no valid callback as first parameter. Let's pick this apart:
call_user_func_array (new $class, $args);
与
$obj = new $class;
call_user_func_array ($obj, $args);
正如你所看到的, $ class
已在调用 call_user_func_array
之前调用。由于没有参数,您将看到此错误消息:
As you can see, the constructor of $class
has been already called before call_user_func_array
comes into action. As it has no parameters, you see this error message:
Missing argument 1 for Product::__construct()
接下来, $ obj
有效回调必须是字符串或数组(或例外是非常特殊的对象: Closure
,但这里没有讨论,我只是为了完整性而命名)。
Next to that, $obj
is of type object. A valid callback must be either a string or an array (or exceptionally a very special object: Closure
, but that's out of discussion here, I only name it for completeness).
As $ obj
是一个对象,不是有效的回调,因此您会看到PHP错误消息:
As $obj
is an object and not a valid callback, so you see the PHP error message:
Object of class Product could not be converted to string.
PHP尝试将对象转换为字符串,但不允许。
PHP tries to convert the object to string, which it does not allow.
你可以看到,你不能轻易地创建一个构造函数的回调,因为对象不存在。也许这就是为什么你不能在手册中轻松查找它。
So as you can see, you can't easily create a callback for a constructor, as the object yet not exists. Perhaps that's why you were not able to look it up in the manual easily.
构造函数需要一些特殊的处理:如果你需要传递变量参数到一个类构造函数尚未初始化对象,则可以使用 ReflectionClass
执行此操作: / p>
Constructors need some special dealing here: If you need to pass variable arguments to a class constructor of a not-yet initialize object, you can use the ReflectionClass
to do this:
$ref = new ReflectionClass($class);
$new = $ref->newInstanceArgs($args);
请参阅 ReflectionClass :: newInstanceArgs
这篇关于call_user_func_array传递参数到构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!