Jump to content

[SOLVED] class inside class


Stooney

Recommended Posts

First off, sorry for the huge post, I kept it as small as I can without making it senseless (I hope).  I've recently been reading up on MVC and am trying to work through it and understand it all better.  I've never really used a registry on a website.  Just passed variables around as needed.

 

This may be a design flaw.  I have a registry object which is to be used 'globally' by any other object that needs it.  To make this work without globalizing it I do $object=new Object(&$registry) when I make a new object, and in the contructor it saved the registry object $this->registry=$registry.  The idea is that all of my objects have access to the same registry object, rather than each having it's own version of the registry.  Is there a better way to do this? 

 

My problem is I can't seem to use objects that are in the registry.  I get these errors:

Notice: Undefined property: Registry::$template in C:\xampp\htdocs\stooney\controllers\index.php on line 4
Fatal error: Call to a member function set() on a non-object in C:\xampp\htdocs\stooney\controllers\index.php on line 4

 

With this (template is an object stored in the registry, is the syntax wrong for this?):

<?php
class Controller_Index Extends Controller_Base{
function index(){
	$this->registry->template->set('first_name', 'Dennis');
	$this->registry->template->show('index');
}
}
?>

 

When doing a print_r on $registry while in Controller_Index (above) I see this which leads me to believe that I should be able to use $template's methods:

Registry Object
(
    [vars:private] => Array
        (
            [db_prefix] => stooney_
            [db] => Db Object
                (
                )

            [template] => Template Object
                (
                    [registry:private] => Registry Object
*RECURSION*
                    [vars:private] => Array
                        (
                        )

                )

            [router] => Router Object
                (
                    [registry:private] => Registry Object
*RECURSION*
                    [path:private] => C:\xampp\htdocs\stooney\\controllers\
                    [args:private] => Array
                        (
                        )

                )

        )

)

 

Here's what it looks like:

index.php
[code]
<?php
$registry=new Registry();

//Database object
$db=new Db(...);
$registry->('db', $db);

//Template object
$template=new Template(&$registry);
$registry->set('template', $template);

//Router
$router=new Router(&$registry);
$router->setPath(site_path.'controllers);
$registry->set('router'), $router);

//etc
?>

 

Example of a class

<?php

class Router{
private $registry;
private $path;
private $args=array();

function __construct($registry){
	$this->registry=$registry;
}
//Cut out the methods
?>

[/code]

Link to comment
Share on other sites

This is the basic gist of a singleton (one way to do it anyway):

 

<?php

class singleton {
    static private $isnt = null;
    public GetInst() {
        if($this->inst == null)
            $this->inst = new singleton;

        return $this->inst;
    }
}

$s = singleton::GetInst();
$s->Something();

singleton::GetInst()->Something();

 

 

Both of those would be operating on the same instance.

Link to comment
Share on other sites

I can't seem to make it past this error:

Fatal error: Using $this when not in object context in C:\xampp\htdocs\stooney\includes\classes\Singleton.php on line 6

 

Here's what the class looks like

<?php
class Singleton{
private static $instance=null;

public function getInstance($class){
	if($this->instance==null){
		$this->instance=new $class;
	}
	return $this->instance;
}
}
?>

 

And my usage:

<?php
$registry=Singleton::getInstance('registry');
$registry->set('somekey', 'thevalue');
?>

Link to comment
Share on other sites

That's because you can't use $this in static functions.  *cough corbin cough*  Just kidding.  Anyway, use this class:

 

<?php
class Singleton {
private static $instance;

protected function __construct() { }
protected function __clone() { }
protected function __wakeup() { }

public static function getInstance() {
	if (!(self::$instance instanceof self) {
		self::$instance = new self;
	}
	return self::$instance;
}
}

 

I find that those other functions being added with blank declarations to be slightly more thorough, but it doesn't really matter.

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.