Daughter class does not recognize protected variable of parent class

0

I have a user class that does not see protected variable of class that it inherits the code is the following

abstract class Model{

    protected $conn;

    public function __construct(){
        $conn = Connect::getConnection();
    }
}

Daughter class

class Usuario extends Model{

    public function testaConexao(){
        if($this->conn)
            return 'sucesso';
        else
            return 'falha';
    }
}

call

$user = new Usuario();
echo $user->testaConexao();
//saida: falha

Generating a log within the Model constructor, the variable $ conn is created and is different from false. But when I try to use it in the child class no longer recognizes, it returns null using $ this-> conn;

    
asked by anonymous 17.03.2016 / 05:06

1 answer

0

Silly error, I forgot to refer with $ this, it should be $this->conn = Connect::getConnection(); within the Model constructor.

abstract class Model{

    protected $conn;

    public function __construct(){
        $this->conn = Connect::getConnection();
    }
}
    
17.03.2016 / 05:09