doubi4617 2012-05-03 01:00
浏览 45
已采纳

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

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);
  • 写回答

3条回答 默认 最新

  • douliao8760 2012-05-03 01:41
    关注

    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
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路