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 callparent::getCon()instead of$this->getCon()?– Woss
I didn’t understand the "call the builder of the mother class in comics"... If I call
$this->getCon()inBDI would be calling a method ofBD, and not ofConn, is not?– rbz
No, if
BDinherits ofConn, then it will possess the method. It will callBDonly if you overwrite it.– Woss
Oh true... you don’t have
getCon()inBD. But because it returns the string and not the connection?– rbz
I made using
$this -> getCon()but continuesNULL– rbz