fabianGarga
fabianGarga

Reputation: 383

Undefined Variable PHP Class

I'm trying to debug a class that I made. It always breaks and throw undefined variable on the logs I couldn't find a solution because I don't know what I'm doing wrong, I think it should work but not.

The undefined variable is on the erase() function, not in the show() function

class pepe{
 private $array = array();

 function show(){
   $this->erase();
   print_r($this->array);
 }
 function erase(){
   print_r($this->array); 
 }
}

$o = new pepe();
$s = $o->show();

Upvotes: 4

Views: 138

Answers (1)

Tewdyn
Tewdyn

Reputation: 717

class pepe{

private $array = array();

 

function show(){

$this->erase();

print_r($this->array);

}

function erase(){

print_r($this->array); 

}

}

 

$o = new pepe();

$s = pepe->show();

Why are you calling pepe here? Should be like this:

  class pepe{
    private $array = array();

 function show(){
   $this->erase();
   print_r($this->array);
 }
 function erase(){
   print_r($this->array); 
 }
}

$o = new pepe();
$s = $o->show();

You have to call

$o->show()

because you've assigned pepe to

$o

Upvotes: 2

Related Questions