SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help..getInstance() in front controller

    protected static $_instance = null;

    public static function getInstance()
    {
    if (null === self::$_instance) {
    self::$_instance = new self();
    }

    return self::$_instance;
    }

    Can anybody explain to me what this result when return? self? what is that? a class? i know there is a "parent" available. This class also did not extended or implement any class.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    When getInstance() is called if the class property $_instance is null then $_instance becomes a instance of the class which getInstance() resides. In either case the object is returned. Take a look at the Singleton Pattern for further clarification.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    When getInstance() is called if the class property $_instance is null then $_instance becomes a instance of the class which getInstance() resides. In either case the object is returned. Take a look at the Single Pattern for further clarification.
    Pretty sure Oddz meant for you to take a look at the Singleton Pattern.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    self is a special keyword which represents the class in which the keyword appears in the source code. It's value is computed at compile time, not runtime. Think of php as going through your source code and replacing self with the name of the class.
    http://www.php.net/lsb


    PHP Code:
    class Foo {
        static 
    $var 'hi';
        static function 
    test() {
            echo 
    self::$var;
            
    // same as
            
    echo Foo::$var;
        }


  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is the usage of singleton pattern? why getInstance use this pattern?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)


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
  •