【发布时间】:2013-06-13 22:13:24
【问题描述】:
假设,我有一个函数,例如:[$data is a stdClass()]
function test_1{
...
...
if (somecondition){
$data->name = NULL;
test_2($data->name);
}
else{
$data->name = 'hello';
test_2($data->name);
}
...
...
}
function test_2($data){
if (!empty($data->name)){
test_3($data->name);
}
else{
test_3();
}
}
function test_3($s = ''){
if (!empty($s)){
//do something
}
else{
$s .= 'World';
}
}
test_3 是带有可选参数的函数。
但是,我收到一个错误:Object of class stdClass could not be converted to string
【问题讨论】:
-
test_1缺少参数,因此此代码甚至无法编译。我怀疑您在此处转录时进行了其他更改,因此我们看不到真正的问题。 -
@Barmar:在这个问题中添加/不添加参数并不重要。问题是关于
test_3。无论如何,我已经编辑了代码。希望你现在能弄清楚。 ;) -
你没有修复
test_1,它仍然缺少参数列表。如果test_1不重要,为什么要包含它? -
这里有些东西没有意义。你调用
test_2($data->name),所以test_2的参数$data是一个字符串,而不是一个对象。但是随后test_2()使用了$data->name,这应该会失败,因为$data不是此函数中的对象。
标签: php function parameter-passing stdclass