PHP - warning - Undefined property: stdClass - fix?

后端 未结 9 1061
梦毁少年i
梦毁少年i 2020-11-30 05:22

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

相关标签:
9条回答
  • 2020-11-30 05:46
    if(isset($response->records))
        print "we've got records!";
    
    0 讨论(0)
  • 2020-11-30 05:48

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

    In this case it would be :

     if( property_exists( get_class($response), 'records' ) ){
           $role_arr = getRole($response->records);
     }
     else
     {
           ...
     }
    
    0 讨论(0)
  • 2020-11-30 05:50

    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.property-exists.php

    More generally if you want all of them:

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

    0 讨论(0)
  • 2020-11-30 05:52

    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);
    }
    
    0 讨论(0)
  • 2020-11-30 05:53

    If think this will work:

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

    newly defined proprties included too.

    0 讨论(0)
  • 2020-11-30 06:02

    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.

    0 讨论(0)
提交回复
热议问题