Notice: Undefined property - how do I avoid that message in PHP?
Asked Answered
C

7

22

Hello I am making this call:

$parts = $structure->parts;

Now $structure only has parts under special circumstances, so the call returns me null. Thats fine with me, I have a if($parts) {...} later in my code. Unfortunately after the code finished running, I get this message:

Notice: Undefined property: stdClass::$parts in ...

How can I suppress this message?

Covin answered 13/4, 2012 at 14:54 Comment(0)
Z
40

The function isset should do exactly what you need.

PHP: isset - Manual

Example:

$parts = (isset($structure->parts) ? $structure->parts : false);
Zoologist answered 13/4, 2012 at 14:59 Comment(0)
S
19

Landed here in 2020 and surprised that noone has mentioned:

1.As of PHP 7.0:

$parts = $structure->parts ?? false;

2.A frowned-upon practice - the stfu operator:

$parts = @$structure->parts;
Swiss answered 7/4, 2020 at 11:10 Comment(0)
S
6

maybe this

$parts = isset($structure->parts) ? $structure->parts : false ;
Spectroscope answered 13/4, 2012 at 15:1 Comment(0)
H
6

With the help of property_exists() you can easily remove "Undefined property" notice from your php file.

Following is the example:

if (property_exists($structure,'parts')) {
    $parts = $structure->parts;
}

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

Hypercorrection answered 4/2, 2015 at 11:44 Comment(1)
Careful with this! This will not cover the case of "magically" accessible properties (via __get), whereas both isset and the null coalescing operator ?? will.Mccleary
L
1

I have written a helper function for multilevel chaining. Let's say you want to do something like $obj1->obj2->obj3->obj4, my helper function returns empty string whenever one of the tiers is not defined or null, so instead of $obj1->obj2->obj3->obj4 you my use MyUtils::nested($obj1, 'obj2', 'obj3', 'obj4'). Also using this helper method will not produce any notices or errors. Syntactically it is not the best, but practically very comfortable.

class MyUtils
{
    // for $obj1->obj2->obj3: MyUtils::nested($obj1, 'obj2', 'obj3')
    // returns '' if some of tiers is null
    public static function nested($obj1, ...$tiers)
    {
        if (!isset($obj1)) return '';
        $a = $obj1;
        for($i = 0; $i < count($tiers); $i++){
            if (isset($a->{$tiers[$i]})) {
                $a = $a->{$tiers[$i]};
            } else {
                return '';
            }
        }
        return $a;
    }
}
Lusitania answered 19/12, 2020 at 7:33 Comment(0)
C
-1

You can turn this off in the php.ini file.. you want to turn off E_NOTICE on the error_reporting flag.

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE

Whether it is wise to do this is another question (to which I suspect the answer is no).

Commodus answered 22/3, 2022 at 1:29 Comment(0)
B
-1

use @ before access to suppress warnings & errors inline:

$v=@$object->field->subfield;

note This is a bad practice, but it will do the trick

Bigner answered 4/8, 2022 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.