I don't seem to be able to access the parent class constructor In PHP.
Here is the code
class Mysql
{
const SERVER, USER, PASSWORD, DB;
function Mysql()
{
return mysqli_connect(self::SERVER, ETC);
}
}
class Mysql_Select extends Mysql
{
function Mysql_Select()
{
$conn = new Mysql();
etc
}
}
I'm trying to keep the actual connection in the parent class, but I get the non-object error.
Best How To :
You're doing it wrong
First thing is that PHP4-style constructors are deprecated, use __construct
method to define a costructor.
Second is that you must not return from constructor but rather keep the connection object inside class as instance property.
Third is that you must call parent methods using parent
keyword.
class Mysql
{
const SERVER, USER, PASSWORD, DB;
protected $conn;
function __construct()
{
$this->conn = mysqli_connect(self::SERVER, ETC);
}
}
class Mysql_Select extends Mysql
{
function __construct()
{
parent::__construct();
// use $this->conn here from the parent class.
}
}