3

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

null我想知道如果没有找到属性,我是否可以让函数返回?

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

编辑:

决定把上面的那个函数变成一个类,但还是不能__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 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);
4

3 回答 3

2

将约翰关于 __get() 的答案结合在一起:

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

?>
于 2012-05-03T01:41:17.273 回答
2

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

对于 PHP 7:

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

对于 PHP 5:

public function __get($prop){
    return isset($this->$prop) ? $this->$prop: null;
}
于 2018-01-04T18:33:48.540 回答
0

未经测试,但我认为使用魔法方法__get()可能在这里工作:

public function __get($parameter)
{
    return (isset($this->$parameter)) ? $this->$parameter : null;
}
于 2012-05-03T01:07:10.263 回答