Re: PHP (Extending Classes)
Update:
I've come to the conclusion that this is best way. But I'm just curious as to why this works and the other option I had in mind isn't working. So the class below is the one that works perfectly and I can use it across all classes.
PHP Code:
class Database
{
public static $connection;
private $connected = false;
private $hostname;
private $username;
private $password;
private $database;
public function __construct($hostname, $username, $password, $database)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
self::$connection = new mysqli($this->hostname, $this->username, $this->password, $this->database);
}
}
I can then call the static variable connection and I'm able to use it. However, let's say I want to extend the mysqli class and use the parents (mysqli class) class constructor to do that, so it's looks even more classy. However, it won't work, here it is.
PHP Code:
class Database extends mysqli
{
public static $connection;
private $connected = false;
private $hostname;
private $username;
private $password;
private $database;
public function __construct($hostname, $username, $password, $database)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
self::$connection = parent::__construct($this->hostname, $this->username, $this->password, $this->database);
}
}
It just returns null and literally the $connection variable just doesn't get set, so it's safe to say it's not accessing the parents class correctly. Anyhow, I'm just curious to why it won't work, however I'm not using it. So don't stress.
But what does everyone think of the top class, that's the one I'm thinking of using?