我的代码有问题 我正在购物车,添加产品时会话中出现错误
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Carrinho::$Carrinhocompra
Filename: loja/Carrinho.php
Line Number: 31
Backtrace:
File: C:\Users\Dell\Desktop\loja_virtual_V1.4\application\controllers\loja\Carrinho.php
Line: 31
Function: _error_handler
File: C:\Users\Dell\Desktop\loja_virtual_V1.4\index.php
Line: 315
Function: require_once
aqui esta o codigo
minha library
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Carrinhocompra {
public function __construct(){
if (!isset($_SESSION['Carrinho'])) {
$_SESSION['Carrinho'] = [];
}
}
// ADICIONAR UM PRODUTO Carrinho
public function add($id, $qtd){
if ( isset($_SESSION['Carrinho'][$id]) ) {
$_SESSION['Carrinho'][$id] = $_SESSION['Carrinho'][$id] + $qtd;
}
else {
$_SESSION['Carrinho'][$id] = $qtd;
}
}
}
meu controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Carrinho extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Loja/Loja_model');
$this->load->library('Carrinhocompra');
}
public function index(){
echo '<pre>';
print_r($_SESSION['Carrinho']);
exit;
$query = $this->Loja_model->getDadosLoja();
$data['titulo'] = 'Carrinho de compras';
$data['dados_loja'] = $query;
$data['categorias'] = $this->Loja_model->getCategoriaLoja();
$data['redes'] = $this->Loja_model->getRedeSociais();
$data['view'] = 'loja/carrinho/listar';
$this->load->view('loja/index', $data);
}
public function add(){
$this->Carrinhocompra->add(1, 5);
}
}