PHP - Fatal error: Using $this when not in object contexto

 
Vista:
sin imagen de perfil

Fatal error: Using $this when not in object contexto

Publicado por hermes augusto (1 intervención) el 16/10/2017 11:34:06
me podrias ayudar llevo mas des tres días despierto creo que mi cerebro ya no cuadra al tratar de es un carrito de compras atraves de ajax y php el detalle es que cuando ejecito al querer agregar me indica un problema con esto lel problema lo arroja al querer ejecutar la funcion update_cart();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
 
 require_once 'Conexion.php';
class Car{
        public $cart = array();
	    public function __construct(){
	    	if(isset($_SESSION['cart'])){
	    		 $this->cart = $_SESSION['cart'];
	    	}
	    }
        public function addCar($c_articulo){
            $status = 0;
            $conexion = new Conexion ();
            $consulta = $conexion->prepare('SELECT * FROM articulos WHERE clave_articulo = :id');
            $consulta->bindParam(':id',$c_articulo);
            $consulta->execute();
            $search = $consulta->fetchAll();
            foreach($search as $info){
                $id = $info['clave_articulo'];
                $descripcion = $info['descripcion'];
                $modelo = $info['modelo'];
                $precio_a = $info['precio'];
                $existencia = $info['existencia'];
                $status++;
            }
 
            $conf = $conexion->prepare('SELECT * FROM confgen ');
            $conf->execute();
            $row = $conf->fetchAll();
            foreach($row as $credito){
                $financiamiento = $credito['t_financiamiento'];
                $eganche = $credito['enganche'];
                $p_maximo = $credito['p_maximo'];
            }
            (float)$precio = $precio_a*(1+($financiamiento*$p_maximo)/100);
			if($status > 0){
				$cantidad='';
				$importe='';
				$item = array(
					'c_artticulo' =>$id,
					'descripcion' =>$descripcion,
					'modelo' =>$modelo,
					'precio' =>$precio,
					'cantidad' =>$cantidad,
					'importe' =>$importe
				);
				if(!empty($cart)){
					foreach ($this->cart as $key){
						if($key['c_artticulo'] == $id){
							$item['catidad'] = $key['catidad'] + $item['catidad'];
						}
					}
				}
 
 
				$folio = md5($id);
				$_SESSION['cart'][$folio] = $item;
				$this->update_cart();
			}
		}
        public function getCar(){
	    	$html = '';
	    	if(!empty($cart)){
	    		foreach ($this->cart as $key){
	    			$folio = "'".$key['c_artticulo']."'";
					$html .= '<tr>
								<td>'.$key['descripcion'].'</td>
								<td>'.$key['modelo'].'</td>
                                <td align="right">'.$key['cantidad'].'</td>
								<td align="right">$ '.number_format($key['precio'], 2).'</td>
								
								<td align="right">'.number_format($key['importe'], 2).'</td>
								<td>
									<button onClick="deleteProduct('.$folio.');">
				                    	Eliminar
				                    </button>
								</td>	
							  </tr>';
				}
	    	}
	    	return $html;
	    }
        public function update_cart(){
			self::__construct();
		}
    }
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Fatal error: Using $this when not in object contexto

Publicado por xve (6935 intervenciones) el 16/10/2017 19:36:32
En que linea te da el error?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de kip
Val: 2.325
Plata
Ha disminuido 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

Fatal error: Using $this when not in object contexto

Publicado por kip (877 intervenciones) el 16/10/2017 23:09:05
Hola, no coloques esa lógica en el constructor, son lineas que se ejecutaran mas de una vez y es mejor colocarlas dentro del metodo update_cart, en el constructor llamar a este metodo y listo no tendras aquel problema de contexto, ya que si te fijas es porque haces una llamada al constructor como metodo estatico y estos no tienen acceso a la variable global $this y ademas la propiedad no la estas declarando como estatica, en fin es mejor hacer solo el consejo del inicio y listo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
 
require_once 'Conexion.php';
class Car{
    public $cart = array();
    public function __construct(){
		$this->update_cart();
    }
    public function addCar($c_articulo){
        $status = 0;
        $conexion = new Conexion ();
        $consulta = $conexion->prepare('SELECT * FROM articulos WHERE clave_articulo = :id');
        $consulta->bindParam(':id',$c_articulo);
        $consulta->execute();
        $search = $consulta->fetchAll();
        foreach($search as $info){
            $id = $info['clave_articulo'];
            $descripcion = $info['descripcion'];
            $modelo = $info['modelo'];
            $precio_a = $info['precio'];
            $existencia = $info['existencia'];
            $status++;
        }
 
        $conf = $conexion->prepare('SELECT * FROM confgen ');
        $conf->execute();
        $row = $conf->fetchAll();
        foreach($row as $credito){
            $financiamiento = $credito['t_financiamiento'];
            $eganche = $credito['enganche'];
            $p_maximo = $credito['p_maximo'];
        }
        (float)$precio = $precio_a*(1+($financiamiento*$p_maximo)/100);
		if($status > 0){
			$cantidad='';
			$importe='';
			$item = array(
				'c_artticulo' =>$id,
				'descripcion' =>$descripcion,
				'modelo' =>$modelo,
				'precio' =>$precio,
				'cantidad' =>$cantidad,
				'importe' =>$importe
			);
			if(!empty($cart)){
				foreach ($this->cart as $key){
					if($key['c_artticulo'] == $id){
						$item['catidad'] = $key['catidad'] + $item['catidad'];
					}
				}
			}
 
 
			$folio = md5($id);
			$_SESSION['cart'][$folio] = $item;
			$this->update_cart();
		}
	}
    public function getCar(){
		$html = '';
		if(!empty($cart)){
			foreach ($this->cart as $key){
				$folio = "'".$key['c_artticulo']."'";
				$html .= '<tr>
					<td>'.$key['descripcion'].'</td>
					<td>'.$key['modelo'].'</td>
                    <td align="right">'.$key['cantidad'].'</td>
					<td align="right">$ '.number_format($key['precio'], 2).'</td>
					
					<td align="right">'.number_format($key['importe'], 2).'</td>
					<td>
						<button onClick="deleteProduct('.$folio.');">
							Eliminar
	                    </button>
					</td>	
				  </tr>';
			}
		}
		return $html;
    }
    public function update_cart(){
		if(isset($_SESSION['cart'])){
			 $this->cart = $_SESSION['cart'];
		}
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar