PHP - 警告 - 未定义的属性:stdClass - 修复了吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2471605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:36:09  来源:igfitidea点击:

PHP - warning - Undefined property: stdClass - fix?

phpobjectwarnings

提问by Phill Pafford

I get this warning in my error logs and wanted to know how to correct this issues in my code.

我在错误日志中收到此警告,并想知道如何在我的代码中更正此问题。

Warning: PHP Notice: Undefined property: stdClass::$records in script.php on line 440

警告:PHP 注意:未定义的属性:第 440 行 script.php 中的 stdClass::$records

Some Code:

一些代码:

// Parse object to get account id's
// The response doesn't have the records attribute sometimes.
$role_arr = getRole($response->records);  // Line 440 

Response if records exists

如果记录存在则响应

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => User
                    [Id] =>
                    [any] => stdClass Object
                        (
                            [type] => My Role
                            [Id] =>
                            [any] => <sf:Name>My Name</sf:Name>
                        )

                )

        )

    [size] => 1
)

Response if records does not exist

如果记录不存在则响应

stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [size] => 0
)

I was thinking something like array_key_exists() functionality but for objects, anything? or am I going about this the wrong way?

我在想类似 array_key_exists() 功能的东西,但对于对象,什么?还是我以错误的方式解决这个问题?

回答by user187291

if(isset($response->records))
    print "we've got records!";

回答by dazweeja

In this case, I would use:

在这种情况下,我会使用:

if (!empty($response->records)) {
 // do something
}

You won't get any ugly notices if the property doesn't exist, and you'll know you've actually got some records to work with, ie. $response->records is not an empty array, NULL, FALSE, or any other empty values.

如果该属性不存在,您将不会收到任何丑陋的通知,并且您会知道您实际上有一些记录可以使用,即。$response->records 不是空数组、NULL、FALSE 或任何其他空值。

回答by markdwhite

isset() is fine for top level, but empty() is much more useful to find whether nested values are set. Eg:

isset() 适用于顶层,但 empty() 在查找是否设置了嵌套值时更有用。例如:

if(isset($json['foo'] && isset($json['foo']['bar'])) {
    $value = $json['foo']['bar']
}

Or:

或者:

if (!empty($json['foo']['bar']) {
    $value = $json['foo']['bar']
}

回答by Astucieux

If you want to use property_exists, you'll need to get the name of the class with get_class()

如果要使用property_exists,则需要使用get_class()

In this case it would be :

在这种情况下,它将是:

 if( property_exists( get_class($response), 'records' ) ){
       $role_arr = getRole($response->records);
 }
 else
 {
       ...
 }

回答by pinaki

The response itself seems to have the size of the records. You can use that to check if records exist. Something like:

响应本身似乎具有记录的大小。您可以使用它来检查记录是否存在。就像是:

if($response->size > 0){
    $role_arr = getRole($response->records);
}

回答by Melsi

If think this will work:

如果认为这会起作用:

if(sizeof($response->records)>0)
$role_arr = getRole($response->records);

newly defined proprties included too.

新定义的属性也包括在内。

回答by AvatarKava

Depending on whether you're looking for a member or method, you can use either of these two functions to see if a member/method exists in a particular object:

根据您是在寻找成员还是方法,您可以使用这两个函数中的任何一个来查看特定对象中是否存在成员/方法:

http://php.net/manual/en/function.method-exists.php

http://php.net/manual/en/function.method-exists.php

http://php.net/manual/en/function.property-exists.php

http://php.net/manual/en/function.property-exists.php

More generally if you want all of them:

更一般地说,如果你想要所有这些:

http://php.net/manual/en/function.get-object-vars.php

http://php.net/manual/en/function.get-object-vars.php

回答by Cray

Error control operator

错误控制运算符

In case the warning is expected you can use the error control operator@to suppress thrown messages.

如果预期会出现警告,您可以使用错误控制运算符@来抑制抛出的消息。

$role_arr = getRole(@$response->records);

While this reduces clutter in your code you should use it with caution as it may make debugging future errors harder. An example where using @may be useful is when creating an object from user input and running it through a validation method before using it in further logic.

虽然这可以减少代码中的混乱,但您应该谨慎使用它,因为它可能会使调试未来的错误变得更加困难。using@可能有用的一个例子是从用户输入创建一个对象并在进一步的逻辑中使用它之前通过验证方法运行它。



Null Coalesce Operator

空合并算子

Another alternative is using the isset_ternaryoperator ??. This allows you to avoid warnings and assign default value in a short one line fashion.

另一种选择是使用isset_ternary运算符??。这允许您避免警告并以短的一行方式分配默认值。

$role_arr = getRole($response->records ?? null);