georgerobbo Posted April 3, 2011 Share Posted April 3, 2011 Hello, I'm attempting to build a Model View Controller and I've run into some issues with inheriting objects / extending classes. The application is focused around a "SuperObject", which contain instantiated libraries, helpers, controllers, models and views. I current have a Base class, which is effectively the "SuperObject". Previously I called the function GetInstance() within my model to get an instance of the SuperObject and assign it to a variable. So, $this->Instance->(The Object)->(The Method)(); However, I want to remove the need for that variable $this->Instance and just have $this->(The Object)->(TheMethod)(); And so, If you see my Controller class, libraries are instantiated as $this->Object which works perfectly. If from, within my controller I was to load the Model class. For example, $this->Loader->Library('Model'); when that model becomes instantiated, even though an extension of the Base class, I cannot use the libraries I instantiated in my controller. And of course there is a Fatal Error: Fatal error: Call to a member function UserAgent() on a non-object <?php class Base { private static $Instance; public function Base() { self::$Instance =& $this; } public static function &GetInstance() { return self::$Instance; } } function &GetInstance() { return Base::GetInstance(); } <?php class Controller extends Base { function Controller() { parent::Base(); $this->Initialize(); } function Initialize() { $Array = array( 'Uri', 'Loader', 'Router', 'Database', ); foreach($Array as $Object) { $this->$Object = loadClass($Object); } } } [/Code] [Code] <?php class Model extends Base { function Model() { parent::Base(); echo $this->Input->UserAgent(); } } [/Code] [Code] <?php class Home extends Controller { function Home() { parent::Controller(); } function index() { $this->Loader->Library('Session'); $this->Loader->Library('Model'); $Data = array( 'Controller' => $this->Router->FetchClass(), 'Title' => ucfirst($this->Router->FetchClass()) ); $this->Loader->View('meta',$Data); $this->Loader->View('header',$Data); $this->Loader->View('home'); $this->Loader->View('footer'); } } [/Code] Any input would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/232551-object-inheritance-get-instance/ Share on other sites More sharing options...
requinix Posted April 3, 2011 Share Posted April 3, 2011 To address the second problem, I don't see you defining an ->Input anywhere. There's not enough code to really address the first problem. Like you haven't posted how Loader or loadClass() work. The source of the problem could be there... Or it could be with your misunderstanding of how static member variables work. There is only one $Instance in your entire application. No, there isn't a different one in each subclass of Base. Just the one defined in Base. So when you construct an object that inherits from Base (which sounds like every object (which is a bad idea anyways)) you'll overwrite $Instance with the newest object. GetInstance() will thus return the latest object from anywhere, not the singleton of that subclass. PHP Quote Link to comment https://forums.phpfreaks.com/topic/232551-object-inheritance-get-instance/#findComment-1196192 Share on other sites More sharing options...
j9sjam3 Posted April 3, 2011 Share Posted April 3, 2011 Read this It will really help with what your doing. Quote Link to comment https://forums.phpfreaks.com/topic/232551-object-inheritance-get-instance/#findComment-1196217 Share on other sites More sharing options...
KevinM1 Posted April 3, 2011 Share Posted April 3, 2011 You shouldn't pass your objects by reference. PHP 5 automatically does it for you, and, really, there's no good reason to not use PHP 5 at this point. It's at least 5 years old now. Also, you're relying way too heavily on the Singleton pattern. Singletons should be used sparingly, if at all, as they're global. There's no need to break scope for this. What you should do is start with your routing system. You need a front controller sitting on top of your system to intercept requests. MVC controllers are really command objects - they're instantiated, and invoke certain functionality, based on the incoming HTTP request. They obtain the correct model from a repository based on GET values, and essentially act as a DI container for both the model and the view. Quote Link to comment https://forums.phpfreaks.com/topic/232551-object-inheritance-get-instance/#findComment-1196367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.