严格的标准:从空值创建默认对象

时间:2012-05-18 13:05:40

标签: php

我有以下代码。

        public function get_users_theme($uid)
            {
                    $r = $this->db2->query('SELECT * FROM users_theme WHERE user_id="'.$uid.'" LIMIT 1', FALSE);
                    if ($this->db2->num_rows($r) > 0) {
                            $o->user_theme = new stdClass();
                            $ut = $this->db2->fetch_object($r);
                            foreach($ut as $l=>$s) {
                                    $o->user_theme->$l  = stripslashes($s);
            }
                            return $o->user_theme;
                    } else {
                            return FALSE;
                    }
            }

第5行似乎产生以下错误:

  

严格标准:从空值创建默认对象

我该如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:4)

当你开始将它用作对象时,

$o尚未声明,所以请事先将其设为stdClass对象:

$o = new stdClass();
$o->user_theme = new stdClass();

或者更好的是,用$o->user_theme替换该函数中的$user_theme - 因为您的代码似乎不需要特殊对象,所以正常变量就足够了。