phpoopmultidimensional-arrayphp-5.3stdclass

How to return null inside the function/method if a property in the class is not found?


I use stdClass to convert an array to an object,

function array_to_object($array)
{
    if(!is_array($array)) {
        return $array;
    }

    $object = new stdClass();
    foreach($array as $key => $value)
    {
        $key = (string) $key ;
        $object->$key = is_array($value) ? array_to_object($value) : $value;
    }

    return $object;
}

$type = array(
    "category"  => "admin",
    "person"    => "unique"
);

$type = array_to_object($type);

var_dump($type->category); // string(5) "admin" 

and of course an error when I want to get the property which is not set in the array in the first place,

var_dump($type->image);

error message,

Notice: Undefined property: stdClass::$image in C:\wamp\www\test\2012\php\array_to_object.php on line 52
NULL

I wonder if I can make the function to return null if no property is found?

 var_dump($type->image); //NULL

EDIT:

Decided to make that function above into a class, but still cannot get __get() working properly,

class objectify
{
    public function __get($name)
    {
        if (isset($this->$name) === true){
            return $this->$name;
        } else {
            return null;
        }
    }

    public function array_to_object($array)
    {
        if(!is_array($array)) {
            return $array;
        }

        $object = self::__get($name);
        foreach($array as $key => $value)
        {
            $key = (string) $key ;
            $object->$key = is_array($value) ? self::array_to_object($value) : $value;
        }
        return $object;
    }
}


$object = new objectify();

$type = array(
    "category"  => "admin",
    "person"    => "unique"
);

$type = $object->array_to_object($type);
var_dump($type->category);
var_dump($type->image);

error message,

Notice: Undefined variable: name in C:\wamp\www\test\2012\php\array_to_object.php on line 85
string(5) "admin" 
Notice: Undefined property: stdClass::$image in C:\wamp\www\test\2012\php\array_to_object.php on line 107
NULL

I think this line is where the error from but I don't know what to do with it...

$object = self::__get($name);

Solution

  • Bringing John's answer about __get() together:

    <? //PHP 5.3+
    
    class maybeBag {
        public function __get($name){
            if (isset($this->$name) === true){
                return $this->$name;
            } else {
                return null;
            }
        }
    
        public static function ensureIsObject($values){
            if (\is_array($values) !== true){
                return $values;
            }
            $o = new static(); //Late-bound make instance of own class
            foreach($values as $key => $value){
                $o->$key = static::ensureIsObject($value);
            }
            return $o;
        }
    }
    
    //Demo
    
    $type = array(
        'category' => 'admin',
        'person'   => 'unique'
    );
    $type = maybeBag::ensureIsObject($type);
    
    var_dump($type->category); //string(5) "admin"
    var_dump($type->image); //NULL
    
    ?>