I'm having a hard time with this. I can not seem to figure out why I can not gain access to a class I'm preloading. I can access it at a particular level, but I don't know why. There is some code here...I hope it will make some sense (I've put some notes into the code):
(I note the error message in the code):
// controller class loads up our tools
// and creates a nice way of carrying around the parts
class Controller {
var $_is_loaded = array();
function Controller()
{
$this->assign_core();
}
function assign_core()
{
// a registry for classes loaded using our tools
// loads both our 'Access' and 'Db' class at the same time
foreach (array('language','template','db','access') as $val)
{
$class = strtolower($val);
if (!is_object($class)) $this->$class =& _load_class($val);
$this->_is_loaded[] = $class;
}
$this->_is_loaded[] = 'load';
}
}
// this is how we grab our objects for usage
function &get_instance()
{
global $OBJ, $INDX;
if (is_object($INDX))
{
return $INDX;
}
else
{
return $OBJ;
}
}
// problem is that here we can not access our methods
// the database won't work
/** -------- ERROR MESSAGE ------------------------------
Fatal error: Call to a member function on a non-object in
/Library/Apache2/htdocs/indx/access.php on line 154 (noted below)
*/
class Router extends Controller
{
function Router()
{
parent::Controller();
$this->access->checkLogin(); // line 154 - ERROR - will not work!
}
}
// but here it does work...
class Page extends Router
{
function Page()
{
parent::Router();
$this->access->checkLogin(); // works!
}
}
// this is generically what is happening in the Access class
class Access
{
function checkLogin()
{
$obj =& get_instance(); // this is the problem
$results = $obj->db->fetchArray(" ...etc... ");
return $results;
}
}
// guess I should add the _load_class function - I bet somebody asks for it
// this is still a work in progress definitely...
function &_load_class($class, $instantiate = TRUE, $internal = FALSE, $place = '')
{
static $objects = array();
if (!isset($objects[$class]))
{
if ($internal == FALSE)
{
if (file_exists($class . '.php'))
{
require($class . '.php');
} else {
echo ' There is an error. ';
exit;
}
if ($instantiate == TRUE)
{
if ($class == 'Controller')
$class = 'Controller';
$objects[$class] =& new $class();
}
else
{
$objects[$class] = TRUE;
}
}
else // TRUE
{
if (file_exists('modules/' . $place . '/index.php'))
{
require('modules/' . $place . '/index.php');
} else {
echo ' There is an error. ';
exit;
}
if ($instantiate == TRUE)
{
$objects[$class] =& new $class();
}
else
{
$objects[$class] = TRUE;
}
}
}
return $objects[$class];
}