Re: OOP PHP with MySQLi
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: OOP PHP with MySQLi

From: Citrus <info@citrus.be>
Date: Tue Jan 31 2006 - 16:44:21 CET

> What a lot of overhead and such.
> I am glad I just use global $connection; whereever I need it.

Where exactly do you find any overhead ? My project will consist out of 15
or more objects with each over 20 or 30 functions. Almost every function
will need to use a database connection. Calling global $connection at the
start of every script and function.. THAT is overhead.

I'm happy with my solution and it's a joy to work with. Currently i'm
working on the foundations wich I feel is very important (i've done 2 major
projects before) and with the upcoming PHP6 i want everything to be done by
the (right) book.

In the meanwhile i've had confirmation that this IS a good way and there
should only be a 'public' statement before function SQL.
So if any people are interested:

<?php

class My {
  private static $connection;

  public function SQL() {
    if(empty(self::$connection)) {
      self::$connection = new mysqli("localhost", "login", "password",
"database");
      if(mysqli_connect_errno()) {
        trigger_error("Could not connect to database !
".mysqli_connect_error()."", E_USER_ERROR);
      }
    }
    return self::$connection;
  }
}

?>

If you load the upper code in every script you can easily comunicate with
your database using
My::SQL()->query();

Best thing is that you can use this in every function or object without
having the need to call anything else.
Received on Tue Feb 7 21:08:21 2006