PHP静态方法变量作用域


PHP static method variable scope

我试图建立一些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的值,它正确地显示了我期望的所有数据。然而,当我从review方法中打印$order变量时,我什么也得不到——一个空的响应。

我不确定为什么值在变化,但我认为这可能是我在变量范围内忽略的东西。有人知道吗?

在您的get_by_token函数中,使用如下:

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