View Single Post
Old 12-05-2007, 04:00 PM   #4 (permalink)
SOCK
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

A bit of constructive criticism, if you don't mind. I understand that you're just starting out with classes and objects.

When you design a class, it should be functionally independent and fluid. You should consider not hard-coding anything like the server address / name into the class, and you should definitely never allow a class to kill a script or interact with the script outside itself.

A better way might be something more like this:
PHP Code:
class MySQL {

    
// variable to store an error message, or FALSE 
    
private $error FALSE;

    
// constructor - remember this cannot return a value!!
    
function __construct($db_host,$db_user,$db_pass,$db_name) {
        
$this->connect($db_host,$db_user,$db_pass);
        
$this->new_db($db_name);
    }

    
// connection method
    
public function connect($host,$user,$pass) {
        
mysql_connect($host,$user,$pass) OR $this->error mysql_error();
    }
    
// db selection method
    
public function new_db($db) {
        
mysql_select_db($db) OR $this->error mysql_error();
    }

    
// error handler
    
public function error_handler() {
        
// assumes no error has occurred
        
if ( $this->error == FALSE ) {
            return 
FALSE;
        } else {
            return 
$this->error;
        }
    }

So in the example class design, you have an error handler method that might be called from the script to check the status. Let the script determine what to do at that point. You might simply want it to log the error or send you an email.

Hope that helps, let me know if you have any questions.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
Wildhoney (12-05-2007)