Undefined PHP Class Variable

I am trying to debug the class that I created. It always breaks and throws the undefined variable into the logs, I could not find a solution, because I do not know what I'm doing wrong, I think that it should work, but no.

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

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();
+4
source share
1 answer
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 here? It 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 need to call

$o->show()

because you assigned pepe to

$o
+2
source

Source: https://habr.com/ru/post/1589130/


All Articles