I have this code which might or might not be well written :). I'm trying to understand why when I create a new instance of one ojbect it doesn't run the parent construct even if I unset it.
class core
{
protected $db;
private static $dsn = 'mysql:host=localhost;dbname=tracking';
private static $user = 'root';
private static $pass = 'root';
private static $instance;
public function __construct () {
$this->db = new PDO(self::$dsn,self::$user,self::$pass);
error_log("database connection called");
}
public static function getInstance(){
if(!isset(self::$instance)){
$object= __CLASS__;
self::$instance=new $object;
}
return self::$instance;
}
}
class Site extends core{
function __construct(){
global $argc, $argv;
$this->db_core = core::getInstance();
$this->pdo = $this->db_core->db;
$this->track_id = $argv[1];
error_log("SITE construct called!");
}
function save_to_db(){
//stuff
}
}
The following code will only log "database connection called" once.
1
$site = new Site;
unset($site);
$site = new Site;
2
$site = new Site;
$site = new Site;
3
$site = new Site;
$site_new = new Site;
The reason why I want the database to be called every time I create a new instance is because this is a cli script that could run for hours inside a while
loop and if the wait_timeout from MySQL is exceeded $site->save_to_db()
just won't do anything because "MySQL server has gone away".
So how does it work? Why wouldn't the parent's construct be called for new instance or when I unset and start it again?