Re: OOP PHP with MySQLi



Erwin Moller wrote:
Citrus wrote:


Hey, i'm looking for the best possible way for connecting to my database
without too much of an effort.

The example works fine and fast. What i need to know is if it's safe,
stable and the way to go. If anyone has a better way for connecting
side-wide to something i'd like you to show me. I've had it working before
using 'global' but people told me 'global' is bad programming.

One of the possibilities would be to make the link and pass it on to every
function and object but this is NOT an option because it makes my script
less transparant and easy to read.


<?php

class My {
 private static $connection;

 function SQL() {
   if(empty(self::$connection)) {
     self::$connection = new mysqli("localhost", "user", "password",
     "db");
   }
   return self::$connection;
 }
}

class User {
 function __construct() {
   echo "user init<p>";
 }

 public function show() {
   $query = My::SQL()->query("SELECT * FROM user");
   while($row = $query->fetch_object()) {
     echo $row->login."<p>";
   }
 }
}

$user = new User();
$user->show();

//Works here too
$query = My::SQL()->query("SELECT * FROM user");
while($row = $query->fetch_object()) {
 echo $row->login."<p>";
}


?>



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

begining of every script that need a db:
<? require_once('dnconnect.php'); ?>

then from a function:
function leaveThingsSimple(){
  global $connection;
  // do stuff here
}

Bad programming?
Says who? And why?


Only use Objects if they add something to your code or structure somehow.
(Like PEAR:DB)
I really don't see the problem with making $connection global...

Regards,
Erwin Moller


Yes, bad programming. It binds all of your code to the variable $connection, among other things. If you need to change that you have a huge amount of code to change. It also makes the code less portable. Also, if $connection gets changed someplace, you'll have a hell of a time trying to debug it.

Globals in general are bad ideas.

Citrus's design is much better. There is a little overhead - but not all that much.

And I find objects almost always improve the structure of my code. It also makes it more flexible and configurable. For instance - I can easily change databases simply by changing the database object. No changes in my code are necessary. Heck - I can even use flat files when necessary!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: OOP PHP with MySQLi
    ... I use it only in functions that need the connection. ... You cannot just 'change database' without one, ... > programming environment. ... Globals in PHP last for the duration of the script whereas the more usual ...
    (comp.lang.php)
  • Re: Cells compared to Flow-Based Programming
    ... see any difference from procedural programming and object oriented ... Same with FBP: When designing a FBP program, you think about data, which is ... from wire x and a value from wire y, first read a value from wire x, then ... In the original FBP concept an outgoing connection feeds only one ...
    (comp.lang.lisp)
  • Re: AS w/ multiple devices, simultaneously
    ... The devices we manufacture don't use ActiveSync for anything so, ... I'm not very familiar with RAPI or programming to ... For breaking the connection, I was thinking of just disconnecting the ... Paul T. ...
    (microsoft.public.pocketpc.activesync)
  • Re: Database, PDO, MySQLi, OOP, S...
    ... Is unstructured programming better then? ... I use mysql connection using mysql_*. ... database functions that you pass a parameter saying which database (and ...
    (comp.lang.php)
  • Re: Cells compared to Flow-Based Programming
    ... flow-based programming is a programming ... the different instances of internal state ... It's the GUI which is diagrammatic. ... The *actual* network definition is a connection list, ...
    (comp.lang.lisp)