Thinkphp引用第三方库的方法例如引用购物车:在项目Home目录下,新建一个文件夹(以下是我自己的demo)
Tool文件夹,在Tool文件夹中创建Tool.class.php文件。这个和你控制器或者模型的创建是一致的。
item = session('?kache') ? session('kache') : array(); } //向购物车添加一个商品 public function add($goods_id,$goods_name,$shop_price){ if($this->item[$goods_id]){ $this->item[$goods_id]['num'] += 1; }else{ $goods['goods_name'] = $goods_name; $goods['shop_price'] = $shop_price; $goods['num'] = 1; $this->item[$goods_id] = $goods; } } //减少购物车一个商品的数量,如果到0则删除此商品 public function decr($goods_id){ if($this->item[$goods_id]){ $this->item[$goods_id]['num'] -= 1; } if($this->item[$goods_id]['num'] <=0){ $this->del($goods_id); } } //从购物车删除某商品 public function del($goods_id){ unset($this->item[$goods_id]); } //列出购物车所有的商品 public function items(){ return $this->item; } //返回购物车有几种物品 public function calcType(){ return count($this->item); } //返回购物车商品的个数 public function calcCnt(){ $n = 0; foreach ($this->item as $k => $v) { $n +=$v['num']; } return $n; } //返回购物车的总价格 public function calcMoney(){ $n = 0; foreach ($this->item as $k => $v) { $n += $v['num'] * $v['shop_price']; } return $n; } //清空购物车 public function clear(){ $this->item = array(); } public function __destruct(){ session('kache',$this->item); }}
创建好购物车需要的类后。在需要的控制器下定义一个方法,引入第三方类库
$tool = \Home\Tool\AddTool::getIns();