Page 1 of 1

Creation Patterns

Posted: Fri Jun 13, 2008 4:54 pm
by blueyon
OK Guys,

I'm stuck on what is the best way to have libraries, models and helpers loaded.

I like how Code Igniter does it, but a lot of other frame works use static loader class that works with __autoload.

Example:

Code: Select all

 
<?php
class Loader {
    static protected $data = array();
    
    static public function register($class, $file) {
        self::data[$class] = $file;
    }
    
    static public function load($class) {
        if (isset(self::data[$class])) {
            include(self::data[$class]);
        } else {
            exit('Error: Could not load ' . $class . '!');
        }
    }
}
 
function __autoload($class) {
    Loader::load($class);
}
?>
 
Loader::register('Config', 'library/config.php');
 
I would like to find the best way to manage dependencies. If I use the method above I normal have to create a model loader class and a helper loader separately.

Also Code Igniter does not cover factories. Some core classes may need some additional tweaking before returning a new object..

What I need is something to cover plain object creation, singletons, factories and storing object states in a registry.

Re: Creation Patterns

Posted: Fri Jun 13, 2008 6:52 pm
by Christopher
Are you looking for general loading of classes, or specifically loading things within an Action Controller?

Re: Creation Patterns

Posted: Fri Jun 13, 2008 7:12 pm
by blueyon
General loading.

I don't think the page controller should have loads of load methods.

so far I have been using something like this:

Code: Select all

 
<?php
class Registry {
    protected $data = array();
        
    public function get($key) {
        if (!isset($this->data[$key])) {
            $this->data[$key] = $this->create($key);
        }
        
        return $this->data[$key];
    }
 
    public function create($key) {
        $method = 'create' . $key;
            
        $args = func_get_args();
            
        array_shift($args);
        
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $args);
        } else {
            exit('Error: Could not create library ' . $key . '!');
        }
    }
 
    private function createCache() {
        if (!is_writable(DIR_CACHE)) {
            exit('Error: Could not write to cache directory!');
        }
            
        require_once(DIR_LIBRARY . 'cache/cache.php');
        
        return new Cache();
    }
            
    private function createConfig() {
        include_once(DIR_LIBRARY . 'config/config.php');
        
        return new Config();
    }
}
?>
 

But this just means I need to create extra classes for loading models and helpers.

Re: Creation Patterns

Posted: Fri Jun 13, 2008 7:37 pm
by Christopher
If you have a finite set of classes that you want to create then having individual creating methods makes sense -- as long as there are not too many. But beyond that you need to move toward a more generalized convention and either do DI or push more initialization duties into the classes themselves.

The other question I have been thinking about recently is how to automate applying configuration information to when classes are instantiated. So using your example, you could have settings for the Cache class in the configuration that would be applied when you create a Cache object.

Re: Creation Patterns

Posted: Sat Jun 14, 2008 7:52 pm
by alex.barylski
The other question I have been thinking about recently is how to automate applying configuration information to when classes are instantiated. So using your example, you could have settings for the Cache class in the configuration that would be applied when you create a Cache object.
How do you mean exactly?

Like using a service locator to fetch an object that hasn't been instantiated yet but you need or want the object initialized already?

Obviously initializing objects with trivial config values is easy, it's when I want to inject one object into another that I have thought about. I haven't actuallly *needed* this yet but it was an interesting excersize.

What solution did you come up with?

Re: Creation Patterns

Posted: Sun Jun 15, 2008 6:17 am
by blueyon
What would happen if you through out all the strict OOP rules and used code like:

$db = DBFactory::create('localhost', 'username', 'password', 'database');
$request = Request::getInstance(); or Request::get('action');

Re: Creation Patterns

Posted: Sun Jun 15, 2008 7:10 pm
by blueyon
I have just come across kohana.

It looks quite intreasting!

http://www.kohanaphp.com

Re: Creation Patterns

Posted: Sun Jun 15, 2008 10:46 pm
by Christopher
Hockey wrote:Like using a service locator to fetch an object that hasn't been instantiated yet but you need or want the object initialized already?
Yes, but that sounds more like DI.
Hockey wrote:Obviously initializing objects with trivial config values is easy, it's when I want to inject one object into another that I have thought about. I haven't actuallly *needed* this yet but it was an interesting excersize.
Yes it is trivial, but it is also annoyingly repetitive. I am thinking more of things like settings for Models, Views, Action Controllers --- standard framework parts...
Hockey wrote:What solution did you come up with?
We are still working on it...