0

this has been driving me nuts (Drinking Obscene amounts of Coffee and working all night doesn't help) I want to gain access to a class from wherever I am within the application. I instantiate the Class within my index page (which auto loads my lib/classes)But it seems I cannot gain global access to it. This is my index page:

function __autoload($class)
{
    require LIBS . $class .'.php';
}

$Core = new Core($server, $user, $pass, $db);

This auto load my Lib/classes perfectly and then I instantiate my Core (This is auto loaded within my Lib/core.php)

Then within my Core is where I create the usual, a database connection, get and check the URL and where I instantiate a few classes (Which are auto loaded) I create a __construct and this is where I want to instantiate a Template class. I wish to have global access for accessing the class within any of my controllers and models.

class Core {

    function __construct(DATABASE VARIABLES IN HERE)
    {
        $this->Template = new Template();
    }

}

Ok so I thought I could access the Template Object by doing the following within my parent model and parent controller:

class Controller
{

    public $Core;

    function __construct()
    {
        global $Core;
        $this->Core = &$Core;
    }
}

The Controller is a parent extends all my controllers, therefore I assumed I could just write $this->Core->Template->get_data(); to access the a Template Method? This Seems to throw an error.

Im sure it must be something simple that I have overlooked, if anyone can give me a hand that would be great. This problem is driving me crazy.

Also a side note within my child controllers within my __construct I construct the Parent parent::_construct();

The Error seems to be Notice: Trying to get property of non-object and Fatal error: Call to a member function get_data() on a non-object.

HireLee
  • 561
  • 1
  • 9
  • 25

2 Answers2

0
class Controller
{

    public $Core;

    function __construct(Core $core)
    {
        $this->Core = $core;
    }
}

class ControllerChild extends Controller {
    function __construct(Core $core, $someOtherStuff){
      parent::__construct($core) ;
      //And your $this->Core will be inherited, because it has public access
    }
}
  • note: You dont have to use & sign when working with objects. Objects are automatically passed by reference.
sybear
  • 7,837
  • 1
  • 22
  • 38
  • Hi @Jari I tried this and the following error occured Catchable fatal error: Argument 1 passed to Controller::__construct() must be an instance of Core, none given, – HireLee Feb 19 '13 at 19:59
  • @LeeMarshall you must pass an instance of Core. Like `new ContollerChild(new Core()) ;` – sybear Feb 19 '13 at 20:03
0

You could make Core a singleton and implement a static function to receive a pointer to the object.

define ('USER', 'username');
define ('PASS', 'password');
define ('DSN', 'dsn');

class Core {

  private static $hInstance; 

  public static function getInstance() { 
    if (!(self::$hInstance instanceof Core)) {
        self::$hInstance = new Core(USER, PASS, DSN);
    } 

    return self::$hInstance; 
  }

  public function __construct($user, $pass, $dsn) {
     echo 'constructed';
  }
 }

Then within your Controller you can use:

$core = Core::getInstance();

Which should output constructed.

Edit

Updated to demonstrate how to construct via the static function w/ output.

Martin
  • 6,632
  • 4
  • 25
  • 28
  • Thanks for the comment, I placed the code within my Core class and put $core = Core::getInstance(); and the following error occured Fatal error: Uncaught exception 'Exception' with message 'Core wasn't constructed yet' in – HireLee Feb 19 '13 at 19:57
  • I get a Fatal Error of cannot redeclare class Index and two warnings of missing arguments 4 and 5 for Core::construct() – HireLee Feb 19 '13 at 20:13
  • I just gave it another try.. The $core = Core::getInstance(); would this go in my parent Controllers __construct? – HireLee Feb 19 '13 at 21:12
  • I use the __construct to check the url and load the appropriate Controller... So if it this www.site.com/index it load the index controller... With trying the code you provided it seems that it constructs but stats a fatal error of Cannot redeclate class Index. – HireLee Feb 19 '13 at 21:21