【问题标题】:Object of class stdClass could not be converted to string (php)类 stdClass 的对象无法转换为字符串 (php)
【发布时间】:2021-11-10 13:03:27
【问题描述】:

我对我网站上的一些更新感到满意,但我不知道我做错了什么。

DB::beginTransaction();
 try {
 Log::debug('--- create ---');
 Log::debug($responsible->contactInfo);
 $firstName  = isset($responsible->firstName)  ? $responsible->firstName  : 'NoName';
 $lastName   = isset($responsible->lastName)   ? $responsible->lastName   : $firstName;
 $middleName = isset($responsible->middleName) ? $responsible->middleName : '';
 if (isset($responsible->contactInfo)) {
 foreach ($responsible->contactInfo as $key => $value) {
 if ($value && property_exists($value, 'type') && $value->type == 'email') {
                   $email = $value->value;
                            }
 if ($value && property_exists($value, 'type') && $value->type == 'phone') {
                   $phone = preg_replace('/[^0-9]/', '', $value->value);
                            }
                        }
                    }

我正在处理此信息:

(
        [id] => 1000005
        [firstName] => Test
        [lastName] => John
        [middleName] => Smith
        [contactInfo] => stdClass Object
            (
                [0] => stdClass Object
                    (
                        [type] => email
                        [value] => testtest@gmail.com
                    )
    
                [1] => stdClass Object
                    (
                        [type] => phone
                        [value] => +380 67 777 7777
                    )
    
            )
    
        [position] => Manager
    )

我得到了这个错误:


    [2021-09-14 15:46:18] production.INFO: responsible  
    [2021-09-14 15:46:18] production.INFO: stdClass Object
    (
        [id] => 1000005
        [firstName] => Test
        [lastName] => John
        [middleName] => Smith
        [contactInfo] => stdClass Object
            (
                [0] => stdClass Object
                    (
                        [type] => email
                        [value] => testtest@gmail.com
                    )
    
                [1] => stdClass Object
                    (
                        [type] => phone
                        [value] => +380 67 777 7777
                    )
    
            )
    
        [position] => Manager
    )
      
    [2021-09-14 15:46:18] production.DEBUG: --- create ---  
    [2021-09-14 15:46:18] production.ERROR: Uncaught exception 'ErrorException' with message 'Object of class stdClass could not be converted to string' in /home/www/test/vendor/monolog/monolog/src/Monolog/Logger.php:329  

 

我希望有人可以帮助我。

【问题讨论】:

  • Log::debug 接受字符串,您是否将其传递给对象?错误信息是这样说的。如果你真的需要像这样调试它,你可以将它转换为 json 字符串,例如,Log::debug(json_encode((array)$responsible->contactInfo))
  • 将对象转成json Log::debug($responsible->contactInfo->toJson());

标签: php arrays json laravel


【解决方案1】:

$responsible->contactInfo 是一个stdClass 实例,它不被记录器接受。

你必须把它转换成一个数组:

 Log::debug($responsible->contactInfo); // error
 Log::debug((array) $responsible->contactInfo); // should works (logged as an array)
 Log::debug(json_encode($responsible->contactInfo)); // should works (logged as json)

【讨论】:

  • 非常感谢大家的帮助。祝你有美好的一天)
猜你喜欢
  • 2011-04-06
  • 2018-10-19
  • 2020-03-16
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多