one object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Nguyen

    one object

    Hello,
    I want to build one object that will be reuse all the time, so I just
    need to create it once for all, how can I do that. A sort of static object.
    Thanks
  • Janwillem Borleffs

    #2
    Re: one object

    David Nguyen wrote:[color=blue]
    > I want to build one object that will be reuse all the time, so I just
    > need to create it once for all, how can I do that. A sort of static
    > object. Thanks[/color]

    Depending on how you want to use it (application or page wide) you could
    settle for a session reference to the object or a singleton class (or a
    combination of both).

    When you do not know what a singleton is, here are some examples:

    PHP 4:



    PHP 5:

    class Singleton {
    private static $_instance = null;

    public static function getInstance () {
    if (self::$_instan ce == null) {
    self::$_instanc e = new self;
    }
    return self::$_instanc e;
    }
    }

    $singleton = Singleton::getI nstance();


    JW



    Comment

    Working...