PHP MySQL Class use

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

    PHP MySQL Class use

    Okay, so I'm looking at making my pages use one connection (link) for
    multiple queries (as it should be done). What I've done so far is
    created a class for the database access which queries can be run
    through, which is straight forward enough, but I'm wondering what would
    be the cleanest and most efficient way of using it.

    I've got multiple classes which need the database connection at any
    point during the page, sometimes for more than one method. Should I pass
    the database object when the other objects are constructed, making it a
    class variable? E.g.

    $object = new Object($db); // pass the mysql object here, store it to be
    // used as $this->db in the object when a query
    // is needed.

    Or should I pass it on a method-by-method basis? E.g.

    $object->requestDetails ($db); // with $db being the mysql object
    $object->deleteDetails( $db); // second use, same db object

    I realise that it'll end up working either way, but is there a standard
    on the way this should be done? I don't want to start coding the project
    and then rewrite it half way through because I don't like the way it works.

    Thanks for the thoughts.

    ~ sock
  • Tony Marston

    #2
    Re: PHP MySQL Class use

    In my infrastructure I have a separate class for each business entity
    (database table) but each of these class instances communicate to the
    database through a single DML class instance. The trick is to create the DML
    instance in a global variable where it can be accessed by any and all
    business entity instances.

    I have put together a sample application (described in
    http://www.tonymarston.net/php-mysql...plication.html) which can be
    run online. You can also download all the source code to see how it is done.

    Hope this helps.

    --
    Tony Marston
    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL


    "erotic sock" <erotic@sock.co m> wrote in message
    news:40c189b0$0 $1587$afc38c87@ news.optusnet.c om.au...[color=blue]
    > Okay, so I'm looking at making my pages use one connection (link) for
    > multiple queries (as it should be done). What I've done so far is
    > created a class for the database access which queries can be run
    > through, which is straight forward enough, but I'm wondering what would
    > be the cleanest and most efficient way of using it.
    >
    > I've got multiple classes which need the database connection at any
    > point during the page, sometimes for more than one method. Should I pass
    > the database object when the other objects are constructed, making it a
    > class variable? E.g.
    >
    > $object = new Object($db); // pass the mysql object here, store it to be
    > // used as $this->db in the object when a query
    > // is needed.
    >
    > Or should I pass it on a method-by-method basis? E.g.
    >
    > $object->requestDetails ($db); // with $db being the mysql object
    > $object->deleteDetails( $db); // second use, same db object
    >
    > I realise that it'll end up working either way, but is there a standard
    > on the way this should be done? I don't want to start coding the project
    > and then rewrite it half way through because I don't like the way it[/color]
    works.[color=blue]
    >
    > Thanks for the thoughts.
    >
    > ~ sock[/color]


    Comment

    Working...