Jump to content

Object Inheritance (Get Instance)


georgerobbo

Recommended Posts

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! :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.