douou0977 2013-06-17 20:31
浏览 39
已采纳

带有可选参数的php函数使用stdClass

Suppose, I have a function such as: [$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 is the function with optional parameters. However, I get an error: Object of class stdClass could not be converted to string

  • 写回答

1条回答 默认 最新

  • dpxpz37157 2013-06-17 20:36
    关注

    I'm assuming you called your function in a manner of the form:

    $data = new stdClass();
    test_3($data);
    

    This fails then as you end up in your else statement, and you can't concatenate a stdClass() to a string (in this case 'World').

    A bit more review suggests that your actual function call is test_3($data->name), and $data->name is likely of stdClass() instead of a string that can be concatenated with 'World'.

    For reference, if you have an error, it'd be helpful to provide the actual line number the error is corresponding to . . . I'm guessing the error is due to the concat, since that's the only place where I see a stdClass() to string conversion would be necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答