View Single Post
Old 08-18-2008, 02:25 AM   #8 (permalink)
Kalle
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

I personally use a mix of the Singleton and Registry pattern with some property overloading.

Example:
PHP Code:
<?php
    
final class Project
    
{
        protected static 
$instance;
        protected static 
$objects    = Array();


        private function 
__construct()
        {
        }

        private function 
__clone()
        {
        }

        public function 
__get($component)
        {
            
$component strtolower((string) $component);

            if(
array_key_exists($componentself::$objects))
            {
                return(
self::$objects[$component]);
            }

            return(
NULL);
        }

        public static function 
init()
        {
            if(!
is_object(self::$instance))
            {
                
self::$instance = new self;
            }

            return(
self::$instance);
        }

        public static function 
load($component)
        {
            
$component strtolower((string) $component);

            if(
array_key_exists($componentself::$objects))
            {
                return(
true);
            }

            
$class 'Project_Component_' $component;

            if(!
class_exists($class))
            {
                throw new 
Project_Exception('Component class not found!');
            }

            
self::$objects[$component] = new $class;

            return(
self::$objects[$component]);
        }
    }

    class 
Project_Exception extends Exception
    
{
    }
?>
This does not introduce component constructor arguments, but its simply a mix. I hope this could help you abit ;)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
Theo (08-18-2008)