I am using PHP 5.4.4 and I have a MySQL Database connection that I want to use for queries in the parent class and have that connection inherited by all children. I create a new instance of the parent and pass the db connection ($handler
) as a parameter and the db connection becomes a property of the parent. When I create a new instance of a child I
thought the db connection would be inherited as a property of the child, however I can only make that happen when I pass it is a parameter to both the child and parent:: constructor functions. This seems redundant to me and I am wondering what I am missing since I am new to PHP OOP.
require "pdo_connect.php"; //mysql db connection
class newUser {
public $timestamp;
public $dbconn;
public function __construct($handler) {
$this->timestamp = time();
$this->dbconn = $handler;
}
public function showdbconn() {
//use $this->dbconn for mysql query
print_r($this->dbconn);
}
}
class social extends newUser {
public function __construct($handler) {
$this->message = 'message';
parent::__construct($handler);
}
public function test() {
//use $this->dbconn for mysql query
print_r($this->dbconn);
}
}
$x = new newUser($handler);
$x->showdbconn();
$y = new social($handler); //why pass to child if passed to parent?
$y->test();