Asked  7 Months ago    Answers:  5   Viewed   47 times

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

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?

 Answers

53
if(isset($response->records))
    print "we've got records!";
Wednesday, March 31, 2021
 
VieStar
answered 7 Months ago
40

PHP compares sequentially (in the order of declaration) the object properties and stops at the first inequal property found. This behavior is not documented, so there's not much to be said about it, sadly, other than looking at the source of PHP.

Not documented is usually a synonym of "don't rely on it".

Wednesday, March 31, 2021
 
rblarsen
answered 7 Months ago
13

I found a solution to the problem: this is a bug in Symfony 2.6, actually 2.7 seem to have fixed it.

If someone still creates a project with composer create-project symfony/framework-standard-edition ..., where the Symfony2 version defaults to 2.6 and experiences the same problem I have faced, here is a workaround:

Either edit YourProject/app/SymfonyRequirements.php line 406 and

YourProject/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/skeleton/app/SymfonyRequirements.php line 406 and replace:

 is_dir($this->getComposerVendorDir()),

With this:

is_dir(__DIR__.'/../vendor/composer'),

This will remove the Major Problem complaining about the vendors. Then this will instead remove the file_get_contents() Warning:

Inside YourProject/app/SymfonyRequirements.php and make line 546 look like this:

file_get_contents(__FILE__) === file_get_contents(__DIR__.'/../vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/skeleton/app/SymfonyRequirements.php'),

Then open localhost/YourProject/web/config.php and you will see the nice Symfony Welcome page again.

enter image description here

Wednesday, March 31, 2021
 
uiroshan
answered 7 Months ago
91

Every time you do $array[] = it inserts a new element in the end of an array. What you need to do is:

class MaterialType {

public $id;
public $name;


function getAllMaterialType() {

    $query = "SELECT * FROM material_types";
    $result = mysql_query($query);

    $arr = array();
    while($row = mysql_fetch_array($result)) {
        $mat = new MaterialType();
        $mat->id = $row['m_type_id'];
        $mat->name = $row['m_type_name'];
        $arr[] = $mat;
    }

    return $arr;
}

}
Wednesday, March 31, 2021
 
Muazam
answered 7 Months ago
11

If you want to avoid the warning you'll need to pre-create each level:

$data = new stdClass();
$data->result = new stdClass();
$data->result->complex = new stdClass();
$data->result->complex->first_key = $old_data->result->complex;
Wednesday, March 31, 2021
 
julesj
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :