dsf5989 2014-05-13 07:54
浏览 23
已采纳

PHP静态方法变量范围[重复]

I'm trying to build some CodeIgniter models but am running into an issue that I can't figure out. I think it may have to do with variable scope, but I'm not sure.

I have a method in my controller that takes a $token value as an argument:

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

        print_r($order);
}

As you can see, I'm calling a static method called get_by_token(). Here's the code for that function:

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');
}

This function just gets the ID of the review from the database based on the token, and then calls another static method to join additional data to the review. Here's the get() function:

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;

}

As you can see, if a $join_class is provided, another static method is called for the $join_class in order to get the data to be joined and add it to the object. The get() method for the join class is here:

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;

}

The issue I am having is that if I print the value of $model just before the first get() function returns, it correctly shows me all of the data I expect. However, when I print $order variable from the review method, I get nothing back - an empty response.

I'm not sure why the value is changing, but I think it is probably something I am overlooking with variable scope. Does anyone have any idea?

</div>
  • 写回答

1条回答 默认 最新

  • dsbj66959 2014-05-13 08:01
    关注

    In your get_by_token function, use this :

    return self::get($model->id, null, null, 'Order_product');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算