Parent::method does not return connection

Asked

Viewed 30 times

0

I have a class Conn connecting to an SQL Server:

class Conn
{
    protected $con;
    protected $a = 'aaa';

    function __construct()
    {
        $this -> conecta();
    }

    private function conecta(){
        $serverName = "xxx.xxx.xxx.xxx";
        $connectionInfo = array( "Database"=>"XXX", "UID"=>"YYY", "PWD"=>"ZZZ", "CharacterSet" => "UTF-8");

        try {
            $conn = sqlsrv_connect($serverName, $connectionInfo);
        } catch (Exception $e) {
            echo "Erro na conexão com o BD.<br/>";
            die( print_r(sqlsrv_errors(), true));
        }

        if (!$conn){
            echo "Erro na conexão com o BD.<br/>";
            die( print_r(sqlsrv_errors(), true));
        } else {
            $this -> con = $conn;
            var_dump($this -> con);
        }
    }

    protected function getCon(){
        return $this -> con;
    }

}

And a class BD that stretches the class Conn:

class BD extends Conn
{
    private $cnx;

    function __construct()
    {
        $this -> cnx = parent::getCon();
        var_dump($this -> cnx);
    }
}

When I create an object of BD:

If in the method getCon() i set return $this -> a;, get back to me: string(3) "aaa"

If in the method getCon() i set return $this -> con;, get back to me: NULL where it should return resource(3) of type (SQL Server Connection)


I wonder what could be causing this or if I’m using it incorrectly.

  • It did not fail to call the constructor of the mother class in BD? And why call parent::getCon() instead of $this->getCon()?

  • I didn’t understand the "call the builder of the mother class in comics"... If I call $this->getCon() in BD I would be calling a method of BD, and not of Conn, is not?

  • No, if BD inherits of Conn, then it will possess the method. It will call BD only if you overwrite it.

  • Oh true... you don’t have getCon() in BD. But because it returns the string and not the connection?

  • I made using $this -> getCon() but continues NULL

1 answer

1


The object $con in Conn is only initialized when the method is called Conn::conecta, which in turn is called in the builder of Conn, but the builder of Conn is never called, for the method was superscripted in the daughter class BD.

If you need to override the constructor, you will need to call the parent constructor explicitly:

class BD
{
    public function __construct()
    {
        parent::__construct();
        // ...
    }
}

Thus, the mother class constructor will be executed, calling the method conecta and initiating the object $con. This way, you can do:

class BD
{
    public function __construct()
    {
        parent::__construct();
        $this->cnx = $this->getCon();
    }
}

Note that it was invoked as $this->getCon(), since, as the method Conn::getCon is protected, it will be inherited in BD as private, being still accessible within the class.

  • Do I always need to call the mother class Construct? Because I renamed it to function Conn()and function BD(), and yet it didn’t work...

  • @RBZ if you overwrite the builder in the daughter class, yes.

  • So, but I didn’t overwrite. I renamed the Conn for function Conn(), so he’s different from function __construct() in BD...

  • 1

    Then it is a method and you will need to invoke it explicitly. A method with the same class name is not a constructor - it was only in PHP 4, but after 5 no more. The only constructor is __construct.

Browser other questions tagged

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