Page 1 of 2

MySQL using OO PHP

Posted: Sun Jul 12, 2009 10:55 am
by MichaelR
This is my first attempt at object-oriented PHP coding, so any critique would be appreciated. I've tested the code and know that it works, but I'm unsure about whether or not the style is good or bad (am I going against best practices?).

Note: After reading the Coding Critique post, I've followed the advice and updated this first post with the newest copy of the code.

Code: Select all

<?php
 
  class MySQL {
 
    private $connection;
 
    public function __construct($array = false) {
 
      if (!is_array($array)) {
 
        $array   = array(
 
          0 => 'hostname',
          1 => 'username',
          2 => 'password',
          3 => 'database',
 
        );
 
      }
 
      $this->connect($array);
 
    }
 
    private function connect($array) {
 
      $this->connection = @mysql_connect($array[0], $array[1], $array[2]);
 
      if (!empty($array[3])) {
        $this->database($array[3]);
      }
 
    }
 
    public function connected() {
      return is_resource($this->connection);
    }
 
    public function database($database) {
 
      if (!$this->connected()) {
        return false;
      }
 
      return @mysql_select_db($database, $this->connection);
 
    }
 
    public function query($query) {
 
      if (!$this->connected()) {
        return false;
      }
 
      return new MySQLResult(@mysql_query($query, $this->connection));
 
    }
 
    public function disconnect() {
 
      if (!$this->connected()) {
        return false;
      }
 
      return @mysql_close($this->connection);
 
    }
 
  }
 
  class MySQLResult {
 
    private $result;
 
    public function __construct($result) {
      $this->result = $result;
    }
 
    public function fetch() {
 
      if (!is_resource($this->result)) {
        return false;
      }
 
      return @mysql_fetch_assoc($this->result);
 
    }
 
  }
 
?>

Re: MySQL using OO PHP

Posted: Mon Jul 13, 2009 3:45 pm
by Darhazer
Your error handling is not OO... Make $connection protected member, and throw exceptions when you cannot connect or execute query.

Re: MySQL using OO PHP

Posted: Sat Aug 01, 2009 11:08 pm
by Pulni4kiya
The normal thing is the MySQL class to extend the Database class or even better - implement some DB interface.
Then in your project you use variables of that interface and can always add other implementations of the interface and change which implementation your project uses.

Re: MySQL using OO PHP

Posted: Wed Aug 05, 2009 8:26 am
by Jenk
Darhazer wrote:Make $connection protected member
Why?

Re: MySQL using OO PHP

Posted: Wed Aug 05, 2009 9:05 am
by m4rw3r
Jenk wrote:
Darhazer wrote:Make $connection protected member
Why?
Because it isn't good if a user can access it directly.
If the user of the class accesses it directly, it means that the user must use database specific functions on that property (eg. mysql_query('SELECT * ...', $db->connection), simple example).

That means that the user must know what that property is, and how it should be treated.
And if you eg. change the db functions in the class, you would end up with a resource which is incompatible with the functions/methods the user of the class applies on that property.

(And if the user replaces that property... *shudder*)

The benefits of a public property is that it can be modified, but if the class is coded properly the user won't need to modify it or access it.

Re: MySQL using OO PHP

Posted: Fri Aug 07, 2009 9:24 am
by towerspl
yep you should for best practice have all variables that you would not like to be directly modified set to private or public

Re: MySQL using OO PHP

Posted: Fri Aug 07, 2009 9:37 am
by jayshields
Don't use the error surpressor operator (@).

Re: MySQL using OO PHP

Posted: Fri Aug 07, 2009 11:56 am
by Christopher
I think I would move query() into the "MySQL" class and have it return a "database" class object as the result. The add Iterator support to that result class.

Re: MySQL using OO PHP

Posted: Mon Aug 17, 2009 2:05 pm
by MichaelR
jayshields wrote:Don't use the error surpressor operator (@).
Why not? I've accounted for a failed connection on line 41. I'd prefer to output my own error reports than use those which are in-built.
Darhazer wrote:Your error handling is not OO... Make $connection protected member, and throw exceptions when you cannot connect or execute query.
The problem I have with this is that I can't see how to make it work for my intended method. I'm going for a completely modular site, where all the content is added through includes. For example:

Code: Select all

 
 
 if (!$database->connection) {
  include('error.php');
 }
 
 else {
  include('home.php');
 }
 
 
Can this be done using thrown exceptions?

Re: MySQL using OO PHP

Posted: Mon Aug 17, 2009 2:19 pm
by Eran
I'd suggest moving to the MySQLi extension

Re: MySQL using OO PHP

Posted: Tue Aug 18, 2009 4:30 am
by MichaelR
Note: Moved to the original post.

Re: MySQL using OO PHP

Posted: Tue Aug 18, 2009 4:37 am
by Eran
It's somewhat pointless to declare static private properties that have no accessors. Define them as class constants instead (and so they will be available to extending classes as well).

Re: MySQL using OO PHP

Posted: Tue Aug 18, 2009 4:49 am
by MichaelR
pytrin wrote:It's somewhat pointless to declare static private properties that have no accessors. Define them as class constants instead (and so they will be available to extending classes as well).
I've changed it slightly since you posted this, but there was a reason for using them as I had. It was only to be used within that class (hence private), but was to be the same throughout every instantiation of it (hence static).

Re: MySQL using OO PHP

Posted: Fri Aug 21, 2009 4:44 am
by Jenk
m4rw3r wrote:
Jenk wrote:
Darhazer wrote:Make $connection protected member
Why?
Because it isn't good if a user can access it directly.
If the user of the class accesses it directly, it means that the user must use database specific functions on that property (eg. mysql_query('SELECT * ...', $db->connection), simple example).

That means that the user must know what that property is, and how it should be treated.
And if you eg. change the db functions in the class, you would end up with a resource which is incompatible with the functions/methods the user of the class applies on that property.

(And if the user replaces that property... *shudder*)

The benefits of a public property is that it can be modified, but if the class is coded properly the user won't need to modify it or access it.
and what if I wanted to share this objects connection with another class/object, but not share the behaviour of this object?

Re: MySQL using OO PHP

Posted: Sat Aug 22, 2009 2:53 pm
by Darhazer
Actually my post was:
Change private $connection to protected $connection
And it have nothing to do with public properties
The difference between private and protected is that if you extends the class, the subclass (the extended class) will still have the property $connection (if the property is protected).
So if you have no good reason to declare something as private, do not do that
Otherwise you or someone else will extend the class, and will have hard time catching why the inherited methods do not work

Edit: since the original code is modified, it's possible that my post was about public => protected, but now I would tell about private => protected