I want to instance an object into other object constructor.
My problem is that the first object MysqlConnector
has a constructor but it doesn't works when I do the instance.
I have debugged the code and there is no way to entry in the constructor of new MysqlConnector();
<?php
class MysqlConnector
{
public $connection;
function __constuct()
{
$this->connection = mysql_connect(Config::MYSQL_HOST, Config::MYSQL_DBUSER, Config::MYSQL_DBPASS);
if(!$this->connection)
{
die("Connection Failed" . mysql_error());
}
mysql_select_db(Config::MYSQL_DBNAME);
mysql_set_charset('utf8');
}
public function __destruct()
{
mysql_close($this->connection);
}
}
?>
And this is the class where I want to create the MysqlConnector
object:
<?php
class Config
{
const DEBUG_MODE = true;
const WEB_URL = "http://localhost:8080/";
const WEB_PATH = "c:/Apache2.2/htdocs/";
const MYSQL_HOST = "localhost";
const MYSQL_DBNAME = "xxx";
const MYSQL_DBUSER = "xxx";
const MYSQL_DBPASS = "xxx";
public function __construct()
{
if(self::DEBUG_MODE)
{
error_reporting(-1);
}
else
{
error_reporting(0);
}
$this->autoLoad();
new MysqlConnector();
}
private function autoLoad()
{
require_once self::WEB_PATH . "db/MysqlConnector.php";
}
}
?>
You have a typo on your class MysqlConnector
It should be
function __construct()
Instead of
function __constuct() //<--- You are missing a `r`
As you have it written now the destructor of MySqlConnector() will run on once __construct() of Config.
You likely want something like $this->db = new MySqlConnector()
Also the typo in function name __construct