Creation Patterns

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Creation Patterns

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creation Patterns

Post by Christopher »

Are you looking for general loading of classes, or specifically loading things within an Action Controller?
(#10850)
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: Creation Patterns

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creation Patterns

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Creation Patterns

Post 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?
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: Creation Patterns

Post 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');
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: Creation Patterns

Post by blueyon »

I have just come across kohana.

It looks quite intreasting!

http://www.kohanaphp.com
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creation Patterns

Post 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...
(#10850)
Post Reply