Php 如果在类中找不到属性,如何在函数/方法中返回null?

Php 如果在类中找不到属性,如何在函数/方法中返回null?,php,oop,multidimensional-array,php-5.3,stdclass,Php,Oop,Multidimensional Array,Php 5.3,Stdclass,我使用stdClass将数组转换为对象 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

我使用
stdClass
将数组转换为对象

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" 
当然,当我想得到数组中没有设置的属性时,会出现一个错误

var_dump($type->image);
错误消息

Notice: Undefined property: stdClass::$image in C:\wamp\www\test\2012\php\array_to_object.php on line 52
NULL
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
我想知道如果未找到属性,是否可以使函数返回
null

 var_dump($type->image); //NULL
编辑:

决定将上面的函数设置为类,但仍然无法使
\uu get()
正常工作

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);
错误消息

Notice: Undefined property: stdClass::$image in C:\wamp\www\test\2012\php\array_to_object.php on line 52
NULL
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
我认为这条线是错误的来源,但我不知道该怎么处理它

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

未经测试,但我认为在这里使用神奇的方法
\uu get()
可能有效:

public function __get($parameter)
{
    return (isset($this->$parameter)) ? $this->$parameter : null;
}

将John关于“聚在一起”的回答:

<? //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

?>

在课堂上使用这个神奇的方法。每当请求未定义的属性时,都会返回null

对于PHP7:

public function __get($prop){
    return $this->$prop ?? null;
}
对于PHP5:

public function __get($prop){
    return isset($this->$prop) ? $this->$prop: null;
}

谢谢,但是我怎样才能只在函数中(而不是在类中)使用_get()?不幸的是,你不能。它只适用于对象。如果你将它添加到你的对象后,它应该工作。对于5.3,上面应该通过删除关键字“static”并使用旧的数组语法(array()而不是[])来工作。是的,我测试了它,它工作了。但是现在我想把函数
array\u to\u object
放到一个类中,我现在被困在这里了。你能看看我的编辑吗?谢谢。一种可能的替代方法是,由于您可能在特定的、狭窄的代码区域(如视图或模板)中生成这些通知,因此暂时将E_通知从php的错误报告级别删除。或者使用eg echo
@$type->category感谢Chris提供此提示!:-)