Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
class Registry{
var $instances;
function register($name, &$object) {
$this->instances[$name] = $object;
}
function get($name) {
return $this->instances[$name];
}
}
class Error{
var $errors = array();
function push($error){
$this->errors[] = $error;
}
}
class MysqlConnect{
function MysqlConnect($host, $user, $pass, &$registry){
$this->host = !empty($host) ? $host : false;
$this->user = !empty($user) ? $user : false;
$this->pass = !empty($pass) ? $pass : false;
$this->error = $registry->get('error');
if(@mysql_pconnect($host, $user, $pass)){
echo "Connected!";
}
else{
$this->error->push(mysql_error());
}
$this->error->push("hi");
}
}
$registry = new Registry;
$error = new Error;
$registry->register('error', $error);
if(!empty($error->errors) && $GLOBALS['debug'] > 0) print_r($error->errors);
If I call $error->push, it is a different instance than if I call $registry->instances['error']->push... shouldn't they be the same since I am calling the objects by reference in my registry?
class Registry
{
static var $instances = array();
function Registry (&$object, $name)
{
self::$instances[$name] = $object;
}
function get ($name)
{
return self::$instances[$name];
}
}
class MysqlConnect{
function MysqlConnect($host, $user, $pass, &$registry){
$this->host = !empty($host) ? $host : false;
$this->user = !empty($user) ? $user : false;
$this->pass = !empty($pass) ? $pass : false;
$this->error = $registry->get('error'); // This is not grabbing the correct instance of $registry or something
if(@mysql_pconnect($host, $user, $pass)){
echo "Connected!";
}
else{
$this->error->push(mysql_error());
}
$this->error->push("hi");
}
}
The mysql object gets instantiated elsewhere in the code... there is no problem there. I intentionally sent it the wrong credentials to see if the error class works... and it doesn't.
Last edited by Luke on Mon Jul 17, 2006 11:03 am, edited 1 time in total.