MySQL using OO PHP

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: MySQL using OO PHP

Post by MichaelR »

Darkhazer wrote: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).

...

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
I've found that if I make $connection a protected member, and try to inherit it in the MySQLResult class, it returns as null, even if returning as a resource in MySQL. I can't work out why this is.
Last edited by MichaelR on Mon Aug 24, 2009 11:38 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: MySQL using OO PHP

Post by Jenk »

Darhazer wrote: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
but my question still stands unanswered.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: MySQL using OO PHP

Post by Darhazer »

I think we've answer both 'why not public' and 'why not private'.
Let me know if further clarification if needed

MichaelR
If you make a property private/protected - you cannot access it directly outside the class. You need to provide a getter for that property (or method that checks the property and returns true/false)
If you throw exceptions, you need to alter the error handling code as well, to catch the exceptions.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: MySQL using OO PHP

Post by MichaelR »

This is an example of what I mean:

Code: Select all

<?php
 
class MySQL {
 
   protected $connection;
 
   public static function connect($host, $username, $password) {
      return new MySQL($host, $username, $password);
   }
 
   private function __construct($host, $username, $password) {
      $this->connection = mysql_connect($host, $username, $password);
   }
 
   public function return_connection() {
 
      if ($this->connection) {
         return $this->connection;
      }
 
   }
 
   public function query($query) {
 
      if ($this->connection) {
         return new MySQLResult($query);
      }
 
   }
 
}
 
class MySQLResult extends MySQL {
 
   private $result;
 
   protected function __construct($query) {
      $this->result = mysql_query($query, $this->return_connection());
   }
 
}
 
$database = MySQL::connect('localhost', 'root', 'password');
 
$user = $database->query("SELECT `username` FROM `database`.`users` WHERE `id` = 1 LIMIT 1");
 
?>
This gives the error code: Warning: mysql_query() expects parameter 2 to be resource, null given in C:\Users\Michael\Websites\htdocs\MySQLClasses.php on line 38.

Edit: I'm an idiot. Please ignore.
Post Reply