I have a Conn class that makes a connection to a 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 BD class that extends the Conn class:
class BD extends Conn
{
private $cnx;
function __construct()
{
$this -> cnx = parent::getCon();
var_dump($this -> cnx);
}
}
When I create a BD object:
If in method getCon()
I set return $this -> a;
, it returns me: string(3) "aaa"
If in method getCon()
I set return $this -> con;
, it returns me: NULL
where it should return resource(3) of type (SQL Server Connection)
I'd like to know what might be causing this or if I'm using it incorrectly.