PHP Singleton Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howachen@gmail.com

    #1

    PHP Singleton Function

    hi,

    in http://hk.php.net/manual/en/language.oop5.patterns.php, it mentions
    the use of "Singleton"func tion. e.g.

    //*************** ******
    public static function singleton()
    {
    if (!isset(self::$ instance)) {
    $c = __CLASS__;
    self::$instance = new $c;
    }

    return self::$instance ;
    }
    //*************** ******

    if this instance use a lot of memory, would it be better if i use
    reference?

    e.g.

    //*************** ******
    public static function &singleton()
    {
    if (!isset(self::$ instance)) {
    $c = __CLASS__;
    self::$instance = new $c;
    }

    return self::$instance ;
    }
    //*************** ******

    any comments? or the php compiler can auto optimize for me?

  • Ryan Lange

    #2
    Re: PHP Singleton Function

    howachen@gmail. com wrote:[color=blue]
    > if this instance use a lot of memory, would it be better if i use
    > reference?
    >
    > e.g.
    >
    > //*************** ******
    > public static function &singleton()
    > {
    > if (!isset(self::$ instance)) {
    > $c = __CLASS__;
    > self::$instance = new $c;
    > }
    >
    > return self::$instance ;
    > }
    > //*************** ******[/color]

    By default, objects in PHP5 are passed/returned by reference, so
    this is unnecessary.

    Ryan

    Comment

    • howachen@gmail.com

      #3
      Re: PHP Singleton Function

      how about PHP4? need explictly use reference?



      Ryan Lange wrote:[color=blue]
      > howachen@gmail. com wrote:[color=green]
      > > if this instance use a lot of memory, would it be better if i use
      > > reference?
      > >
      > > e.g.
      > >
      > > //*************** ******
      > > public static function &singleton()
      > > {
      > > if (!isset(self::$ instance)) {
      > > $c = __CLASS__;
      > > self::$instance = new $c;
      > > }
      > >
      > > return self::$instance ;
      > > }
      > > //*************** ******[/color]
      >
      > By default, objects in PHP5 are passed/returned by reference, so
      > this is unnecessary.
      >
      > Ryan[/color]

      Comment

      • Mladen Gogala

        #4
        Re: PHP Singleton Function

        On Sun, 14 May 2006 11:10:19 -0700, howachen wrote:
        [color=blue]
        > how about PHP4? need explictly use reference?[/color]

        PHP is not Perl. You don't have constructs like \$a and "bless \$a class".
        You can explicitly force access by reference by using something like
        foreach ($array as &$iterator) but you can't manipulate pointers the way
        you do in Perl.

        --
        大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


        Comment

        • Ryan Lange

          #5
          Re: PHP Singleton Function

          howachen@gmail. com wrote:[color=blue]
          > how about PHP4? need explictly use reference?[/color]

          Yeah.

          Ryan

          Comment

          • Oli Filth

            #6
            Re: PHP Singleton Function

            howachen@gmail. com said the following on 14/05/2006 19:10:[color=blue]
            > Ryan Lange wrote:[color=green]
            >> howachen@gmail. com wrote:[color=darkred]
            >>> if this instance use a lot of memory, would it be better if i use
            >>> reference?
            >>>
            >>> e.g.
            >>>
            >>> //*************** ******
            >>> public static function &singleton()
            >>> {
            >>> if (!isset(self::$ instance)) {
            >>> $c = __CLASS__;
            >>> self::$instance = new $c;
            >>> }
            >>>
            >>> return self::$instance ;
            >>> }
            >>> //*************** ******[/color]
            >> By default, objects in PHP5 are passed/returned by reference, so
            >> this is unnecessary.
            >>[/color]
            > how about PHP4? need explictly use reference?
            >[/color]

            Somewhat irrelevant, as PHP 4 doesn't implement static class members.


            --
            Oli

            Comment

            • howachen@gmail.com

              #7
              Re: PHP Singleton Function

              however, php4 support static variable at the function level

              which make Singleton implementation possible, sth like that...

              e.g.

              fucntion getManager() {

              static managerInstance ;

              if (!isset(manager Instance)) {
              managerInstance = new Manager();
              }

              return managerInstance ;

              };

              ....


              Oli Filth 寫道:
              [color=blue]
              > howachen@gmail. com said the following on 14/05/2006 19:10:[color=green]
              > > Ryan Lange wrote:[color=darkred]
              > >> howachen@gmail. com wrote:
              > >>> if this instance use a lot of memory, would it be better if i use
              > >>> reference?
              > >>>
              > >>> e.g.
              > >>>
              > >>> //*************** ******
              > >>> public static function &singleton()
              > >>> {
              > >>> if (!isset(self::$ instance)) {
              > >>> $c = __CLASS__;
              > >>> self::$instance = new $c;
              > >>> }
              > >>>
              > >>> return self::$instance ;
              > >>> }
              > >>> //*************** ******
              > >> By default, objects in PHP5 are passed/returned by reference, so
              > >> this is unnecessary.
              > >>[/color]
              > > how about PHP4? need explictly use reference?
              > >[/color]
              >
              > Somewhat irrelevant, as PHP 4 doesn't implement static class members.
              >
              >
              > --
              > Oli[/color]

              Comment

              Working...