#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,670
    Rep Power
    171

    What does this code do? Is this a design pattern?


    PHP Code:
    class pattern_test 
            
    {
                private 
    $val NULL;
                public function 
    __construct()
                    {
                        if(
    $thi->val==NULL)
                            {
                                
    $this->val = new  pattern_test();
                            }
                        return  
    $this->val;
                    }
            }
            
        
    $o = new pattern_test(); 
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,118
    Rep Power
    1990
    That will end up locking up your system. It will keep on producing another internal object every time an object is created. That will eat up memory pretty quickly.

    There's still some problems with the code. Firstly, there's no point checking if the value is NULL or not. It's specified as NULL in the objects code, and the value can't possibly be accessed before the constructor is called. Secondly, constructors don't return anything, so the return at the end of that is useless.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Looks like you're trying to make a Singleton.
  6. #4
  7. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by requinix
    Looks like you're trying to make a Singleton.
    in which case you need to use self::$val rather than $this->val, eg

    PHP Code:
    //
    class pattern_test {
        private static 
    $val//holds instance

        
    private function __construct() {

        }
        public static function 
    getInstance() {
            if(!isset(
    self::$val)) {
                
    $c __CLASS__;
                
    self::$val = new $c;
            }
            return 
    self::$val;
        }
        
        
    /*
        Define other methods here
        */
        
        
    public function foo() {
            return 
    'bar';
        }
        
    }

    $instance pattern_test::getInstance();

    $instance->foor();

    //or

    pattern_test::getInstance()->foo(); 
    No matter where you use this in your code, you will always get the same instance - almost like a global
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Northie
    No matter where you use this in your code, you will always get the same instance - almost like a global
    It's not a singleton, though, because it can still be cloned as well as duplicated through serialization.

    For a complete implementation of the singleton pattern, check the manual.
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    Originally Posted by Jacques1
    It's not a singleton, though, because it can still be cloned as well as duplicated through serialization.

    For a complete implementation of the singleton pattern, check the manual.
    That used to link to documentation about the singleton pattern but does not any more....

    In normal usage the getInstance function will suffice, but you can add your own __clone method to throw a fatal error to prevent cloning from outside the class
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    *lol* For some reason they don't have the "pattern" chapter on the English website.

    So here's the code on the German page:

    http://php.net/manual/de/language.oo...hp#example-170
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,996
    Rep Power
    375
    Originally Posted by Jacques1
    *lol* For some reason they don't have the "pattern" chapter on the English website.

    So here's the code on the German page:

    http://php.net/manual/de/language.oo...hp#example-170
    if you have chrome, load this on here and click on translate.
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    By the way, using __CLASS__ will lead to weird behaviour if you inherit from the singleton, because it always refers to the class where it's defined rather than the class where it's evaluated from:

    PHP Code:
    <?php

    class {

        public static function 
    getInstance() {
            
    $class =  __CLASS__;
            return new 
    $class();
        }

    }


    class 
    extends {

    }


    $b B::getInstance();
    var_dump$b );            // WTF? It's an instance of A
    So calling getInstance() on a subclass would return an instance of some ancestor rather than of the class itself.

    The solution is to use late static binding through the static keyword (not to be confused with the static modifier):

    PHP Code:
    <?php

    class {

        public static function 
    getInstance() {
            return new static();
        }

    }


    class 
    extends {

    }


    $b B::getInstance();
    var_dump$b );            // It's actually an instance of B
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo