PHP静态方法变量范围

时间:2014-05-13 07:54:48

标签: php codeigniter

我正在尝试构建一些CodeIgniter模型,但遇到了一个我无法弄清楚的问题。我认为这可能与变量范围有关,但我不确定。

我的控制器中有一个方法,它以$token值作为参数:

public function review($token)
{
        $order = Order_model::get_by_token($token, BC_EXT_TYPE_ID, 'Order_product');

        print_r($order);
}

如您所见,我正在调用名为get_by_token()的静态方法。这是该功能的代码:

public static function get_by_token($unique_token, $ext_type_id = NULL, $join_class = NULL, $details = TRUE){

                   $CI =& get_instance();

        $where = array(
                "unique_token" => $unique_token,
                "deleted" => 0,
                "ext_type_id" => $ext_type_id
        );

        $CI->db->where($where);
        $query = $CI->db->get(static::$db_table);
        $model = $query->row(0, get_called_class());

        self::get($model->id, null, null, 'Order_product');
}

此函数仅根据令牌从数据库中获取审阅的ID,然后调用另一个静态方法将其他数据连接到审阅。这是get()函数:

public static function get($id = NULL, $ext_id = NULL, $ext_type_id = NULL, $join_class = NULL, $details = TRUE){

                $CI =& get_instance();

                $where = array(
                        "deleted" => 0
                );

                if(!is_null($id)){
                        $where['id'] = $id;
                }
                elseif(!is_null($ext_type_id) && !is_null($ext_id)){
                        $where['ext_id'] = $ext_id;
                        $where['ext_type_id'] = $ext_type_id;
                }

                $CI->db->where($where);

                $query = $CI->db->get(static::$db_table);
                $model = $query->row(0, get_called_class());

                if(!is_null($join_class)){
                        $joins = $join_class::get($model->id, null, null, $details);
                        $join_property = explode('_', $join_class);
                        $join_property = $join_property[1].'s';
                        $model->$join_property = array();
                        foreach($joins as $join){
                                $model->{$join_property}[] = $join;
                        }

                }

                return $model;

}

如您所见,如果提供$ join_class,则为$ join_class调用另一个静态方法,以便获取要连接的数据并将其添加到对象。联接类的get()方法位于:

public static function get($primary_id, $secondary_id = NULL, $external_type_id = NULL, $details = TRUE){

        $CI =& get_instance();

        $where = array(
                static::$primary_column => $primary_id,
                'deleted'=>0
        );

        if(!is_null($secondary_id)){
                $where[static::$secondary_column] = $secondary_id;
        }

        if(!is_null($external_type_id)){
                $where['ext_type_id'] = $external_type_id;
        }

        $CI->db->where($where);
        $query = $CI->db->get(static::$db_table);

        $result = array();

        foreach($query->result(get_called_class()) as $row){

                if($details){
                        $secondary_model = static::$secondary_model;
                        $secondary_column = static::$secondary_column;
                        $object = $secondary_model::get($row->$secondary_column);
                        $row->details = $object;
                }

                $result[] = $row;
        }

        return $result;

}

我遇到的问题是,如果我在第一个get()函数返回之前打印$ model的值,它会正确显示我期望的所有数据。但是,当我从审核方法中打印$ order变量时,我什么也得不回 - 空响应。

我不确定为什么价值会发生变化,但我认为这可能是我在可变范围内忽略的事情。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

get_by_token功能中,使用此功能:

return self::get($model->id, null, null, 'Order_product');