Child class does not recognize protected mother class variable

Asked

Viewed 20 times

0

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

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';
    }
}

calling for

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

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

1 answer

0

Silly mistake, forgot to reference with $this, must be $this->conn = Connect::getConnection(); inside the Model constructor.

abstract class Model{

    protected $conn;

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.