Factory class question

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

    #1

    Factory class question

    Hello!
    And first of all thanks to all php gurus who helped me a lot last time
    on my question!
    I have another problem now!
    I have taken a Database Connection Factory class to use in my website.
    Normally, I get a singleton of the Database instance.
    What's wrong is that this singleton is initialized for every page
    where i call it.
    If I use the singleton several times in a same php web page, it is
    initialized once only, it works fine.
    But then every time I load a new php web page, if I put some log, I
    can see the singleton is new again and initialized every time!
    I thuoght that the point of a factory class was to initialize only
    once an important variable.
    Did I understand wrong?
    Here is the code (very simple):


    class ConnectionFacto ry
    {

    var $dbAccessor;
    var $host = "myhost";
    var $database = "mydbname";
    var $user = "root";
    var $password = "none";


    function ConnectionFacto ry($host= '', $database = '', $user= '',
    $password='')
    {
    $this->dbAccessor =
    DB::connect('my sql://'.$user.'@'.$pa ssword.'+'.$hos t.'/'.$database);
    }


    function getInstance()
    {

    //single instance
    global $instance;

    if(!isset($inst ance))
    {
    $instance = new ConnectionFacto ry($this->host,
    $this->database,$th is->user, $this->password);
    }

    return($instanc e);
    }

    function getAccessor()
    {
    return $this->dbAccessor;
    }
    }

    -----------

    To use it, I call:

    $singleton =& ConnectionFacto ry::getInstance ();
    $db=$singleton->getAccessor( );

    $row = $db->getRow("sele ct * from user");

    Thanks for your help,
    Bob
  • Virgil Green

    #2
    Re: Factory class question

    "Bob" <robertdeniro@c onsultant.com> wrote in message
    news:de0a9d33.0 407221843.392c5 bf4@posting.goo gle.com...[color=blue]
    > Hello!
    > And first of all thanks to all php gurus who helped me a lot last time
    > on my question!
    > I have another problem now!
    > I have taken a Database Connection Factory class to use in my website.
    > Normally, I get a singleton of the Database instance.
    > What's wrong is that this singleton is initialized for every page
    > where i call it.
    > If I use the singleton several times in a same php web page, it is
    > initialized once only, it works fine.
    > But then every time I load a new php web page, if I put some log, I
    > can see the singleton is new again and initialized every time!
    > I thuoght that the point of a factory class was to initialize only
    > once an important variable.
    > Did I understand wrong?
    > Here is the code (very simple):[/color]

    PHP? Web? everything is new every time you load a page. That's the nature of
    the beast. Sessions, databases, filesystems, cookies, etc. are used to
    oversome the transactional nature of the web.

    Are you, perhaps thinking along the lines of Java and Servlet containers?

    - Virgil


    Comment

    • Chung Leong

      #3
      Re: Factory class question


      "Bob" <robertdeniro@c onsultant.com> wrote in message
      news:de0a9d33.0 407221843.392c5 bf4@posting.goo gle.com...[color=blue]
      > Hello!
      > And first of all thanks to all php gurus who helped me a lot last time
      > on my question!
      > I have another problem now!
      > I have taken a Database Connection Factory class to use in my website.
      > Normally, I get a singleton of the Database instance.
      > What's wrong is that this singleton is initialized for every page
      > where i call it.
      > If I use the singleton several times in a same php web page, it is
      > initialized once only, it works fine.
      > But then every time I load a new php web page, if I put some log, I
      > can see the singleton is new again and initialized every time!
      > I thuoght that the point of a factory class was to initialize only
      > once an important variable.[/color]

      Yes, and that why it's pretty pointless to use class factories in PHP.
      Basically the problem that you're trying to solve is already solved by PHP
      internally and your code has little to do. Just call the appropriate DB
      connection function and let PHP's resource management do its thing.


      Comment

      • Bob

        #4
        Re: Factory class question

        "Virgil Green" <vjg@DESPAMobsy dian.com> wrote in message news:<lXdMc.504 1$YP5.1655@news svr22.news.prod igy.com>...
        [color=blue]
        >
        > PHP? Web? everything is new every time you load a page. That's the nature of
        > the beast. Sessions, databases, filesystems, cookies, etc. are used to
        > oversome the transactional nature of the web.
        >
        > Are you, perhaps thinking along the lines of Java and Servlet containers?
        >[/color]


        Indeed, I've been programming in java for web all my programmer's
        life, so I thought I could find some similar concept in php!

        I wanted to have a factory for some cache of some datas that are
        shared among my website users.
        I wanted to use an object that could contain some datas from a user,
        and others users could get the info, instead of reloading them from
        the database each time.
        What I am going to do is to use Cache_Lite from Pear which seems to be
        good for that job!

        Thank you all for your help!!
        Bob

        Comment

        Working...