SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: registry

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    registry

    I'm trying to get this registry code to work!!

    Its basically loads classes when they are needed, However the error and render class both depend on each other and I have put the code in an infiniate loop.

    This isn't the full code, just a sample I have knocked together.

    I'm not sure how to handle this type of situation. Thanks

    PHP Code:
    <?php

        
    class Loader
        
    {
             private 
    $instances = array();

            public static function &
    getInstance()
            {
                static 
    $me;
                if (
    is_object($me) == false) {
                    
    $me = new Loader;
                }
                return 
    $me;
            }
        
            public function &
    load($class)
            {
                
    // check if class loaded if not load it
                
    if (!array_key_exists($class$this->instances)) {
                    
    $this->instances[$class] = new $class;
                }
                return 
    $this->instances[$class];
            }
        }

        class 
    Render
        
    {
             private 
    $error;

            public function 
    __construct()
            {
                
    $loader      =& Loader::getInstance();
                
    $this->error $loader->load('Error');
            }
            public function 
    display($contents)
            {
                echo 
    $contents;
            }
        }
        
        class 
    Error
        
    {
             private 
    $render;

            public function 
    __construct()
            {
                
    $loader        =& Loader::getInstance();
                
    $this->render $loader->load('Render');
            }
            public function 
    show404($message)
            {
                
    $this->render->display("404");
            } 
        }
        
        class 
    Application
        
    {
             private 
    $error;

            public function 
    __construct()
            {
                
    $loader       =& Loader::getInstance();
                
    $this->render $loader->load('Render');

                
    $this->render->display("application");
            }
        }

        
    $app = new Application;

    ?>

  2. #2
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about moving the registry call out of the constructor? Perhaps to an init() function, which is called after the constructor. Another option would be to lazy-load the dependency, not calling init() until the dependency is needed.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get rid of circular dependency. For example, it's not obvious why Render should depend on Error.

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies. Render depends on error because if a view is not found and error is shown. Error needs the Render class in order to show the error view ie a template file.

    My example does not really show what I am trying to do.

    However I have found that removing the error construct function and loading the dependency from the error display function, I have removed that loop.

    Don't know wheather this is a good or bad example or not.

    PHP Code:
    <?php

        
    class Loader
        
    {
             private 
    $instances = array();

            public static function &
    getInstance()
            {
                static 
    $me;
                if (
    is_object($me) == false) {
                    
    $me = new Loader;
                }
                return 
    $me;
            }
        
            public function &
    load($class)
            {
                
    // check if class loaded if not load it
                
    if (!array_key_exists($class$this->instances)) {
                    
    $this->instances[$class] = new $class;
                }
                return 
    $this->instances[$class];
            }
        }

        class 
    Render
        
    {
             private 
    $error;

            public function 
    __construct()
            {
                
    $loader      =& Loader::getInstance();
                
    $this->error $loader->load('Error');
            }
            public function 
    display($file)
            {
                 if(!
    file_exists($file)) {
                     
    $this->error->display("404.php");
                 } else {
                    include(
    $file);
                }
            }
        }
        
        class 
    Error
        
    {
            public function 
    display($view)
            {
                
    $loader =& Loader::getInstance();
                
    $render $loader->load('Render');
                
    $render->display($view);
            } 
        }
        
        class 
    Application
        
    {
             private 
    $error;

            public function 
    __construct()
            {
                
    $loader       =& Loader::getInstance();
                
    $this->render $loader->load('Render');

                
    $this->render->display("product.php");
            }
        }

        
    $app = new Application;

    ?>

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    > Render depends on error because if a view is not found and error is shown.

    That should be handled from a higher layer in my view; You shouldn't even reach the point of execution of your application logic in the event of a 404. But personally, that is my own view point; It's something your Front Controller should be able to manage, easily.

    However, in your present situation, just pass in the renderer via the class method it's self, along with the message, instead of having to be dependent on the Registry.

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might be interested in Phemto. I tend just to use hard-coded "assembler" classes but a dependency injection container like Phemto is invaluable if you want something to be configurable.

  7. #7
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might be interested in Phemto.
    Thanks I will check it out!

    Get rid of circular dependency
    I think your right, they shouldn't rely on either other and I think I've gotten confused about when and how to display errors.

    If a user requests a controller or action and it doesn't exist, a 404 is dispatched, either as a message or a rendered template/ view.

    If the controller and action exists and a view is requested which doesn't exist, an application error is displayed! Maybe even a trigger_error("View not found: view.php", E_USER_ERROR);

    So, I'm thinking the error class shouldn't render just display the error message and die.

    Thanks again for the replies.

  8. #8
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a 404 error, don't forget to set the proper 404 HTTP response headers. If you don't, your server will still send a 200 OK HTTP response and search engines and the like won't know that a 404 error has occurred.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •