SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37
  1. #26
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JaredWhite
    The beauty of [a Registry object as my only Singleton-type object] is that a singleton-type class can easily be replaced with another one using the same interface without code all over the entire app having to be modified (which is evil!).
    Well, that depends, I suppose, on whether you're changing the API. If singleton creation/accessing only ever takes the form Classname::getInstance() and the methods of the singletons (as opposed to the implementation behind them) are settled early, perhaps it's not such an issue.
    Zealotry is contingent upon 100 posts and addiction 200?

  2. #27
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Santa Rosa, CA
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Certainly, it's not a big issue when it's something you have a lot of overall control over. But when I'm working on something I know will be used and extended by many other people someday, I like to make sure pieces of the puzzle can easily be replaced at various stages.
    Willowgarden: rapid application platform for PHP 5
    xajax: fast and easy PHP Ajax library
    Web software architecture blog: The Idea Basket

  3. #28
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yo?? Dude?? What the...

    Are you going through a midlife crisis Marcus... You have hit the 40s The Registry if I remember, is based on a Singleton it's self so what's the difference of creating a single self purpose Singleton, and a Singleton container?

    None what I can see, you should explain some more, instead of taking the piss. Dude

  4. #29
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    The Registry if I remember, is based on a Singleton it's self so what's the difference of creating a single self purpose Singleton, and a Singleton container?
    The fewer the merrier. Besides - the registry needn't be a singleton. In larger application you often end up having a few "local" registries for some subpart of the application.

  5. #30
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    The fewer the merrier. Besides - the registry needn't be a singleton. In larger application you often end up having a few "local" registries for some subpart of the application.
    Absolutly correct. Consider that Fowler's IdentityMap pattern is essentially a Registry embeded in a Factory.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #31
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Consider that Fowler's IdentityMap pattern is essentially a Registry embeded in a Factory.
    Funny - I had that exact same example in mind when I wrote the above post ...

  7. #32
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this singleton pattern works in PHP 4 too??
    i tried it like this
    PHP Code:
        class configuration
        
    {
            var 
    $cfg = array();
            var 
    $cfgFor '';
            function 
    configuration($cfgFor '')
            {
                
    $this->cfgForm $cfgFor;
            }
            function 
    getInstance()
            {
                static 
    $instance;
                if(
    is_object($instance))
                {    return 
    $instance;    }
                
    $instance =& new $this;
                return 
    $instance;
            }
        } 
    But, i get the following error:
    Notice: Undefined variable: this in C:\project\rattanpal\public_html\myproject\my\newfw\lib\configuration\class.configuration.php on line 17

    Fatal error: Cannot instantiate non-existent class: in C:\project\rattanpal\public_html\myproject\my\newfw\lib\configuration\class.configuration.php on line 17
    What am i doing wrong here? obviously missing some point
    my unittest file is like
    PHP Code:
    <?php
        define
    ('LOAD',TRUE);
        include(
    '../configuration/class.configuration.php');
        
    $obj configuration::getInstance();
    ?>
    ---------------------------
    Errors = Improved Programming.
    My Site

  8. #33
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have several problems.

    1) getInstance should always be called statically, and therefore can't reference $this as the error is telling you
    2) getInstance should return a reference to the object
    3) static variables can not store a reference, therefore you have two options:

    a) don't create by refrence
    PHP Code:
            function &getInstance()
            {
                static 
    $instance;
                if(!
    is_object($instance))
                {    
    $instance = new configuration;    }
                return 
    $instance;
            } 
    b) store an array in the static variable and return a know index by reference
    PHP Code:
            function &getInstance()
            {
                static 
    $instance = array();
                if(!
    $instance)
                {   
    $instance[0] =& new configuration;    }
                return 
    $instance[0];
            } 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #34
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks sweatje, you are a life saver.

    and can you explain what is the diffrence and adv/disadv in a) and b) approach.

    thanks
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #35
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    and can you explain what is the diffrence and adv/disadv in a) and b) approach.
    a is a little simpler to write and would cause another programmer less head scratching when they run across it.
    Edit:

    whoops, I just realized there was a typo in the original posted version of a, the instantiation should not have been caught by reference, I corrected it now


    b it technically more correct in the situation where the contructor for the class passed the $this reference elsewhere and you need to maintain that exact reference. Fortunatly, this is a rare and convoluted case
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  11. #36
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right,
    one more question, you did mentoin it but not clear

    in b)
    if(!$instance)
    { $instance[0] =& new configuration; }
    in a)
    if(!is_object($instance))
    { $instance = new configuration; }

    when you created that object, in b) you created with reference, but in a) dint, why??
    ---------------------------
    Errors = Improved Programming.
    My Site

  12. #37
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    never mind, reread it 5 more times and got it, static variables cant store reference
    thanks
    ---------------------------
    Errors = Improved Programming.
    My Site


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
  •