1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP: Object Oriented Everything - Please review!

Discussion in 'Reviews' started by Will Morgan, Dec 4, 2007.

  1. #1
    I haven't coded PHP in a while, so I was wondering how I could expand my knowledge of OOP through a hands on practice. What I've done is tried to make everything an object; I've not got so far yet, but if any PHP gurus could criticize my code and give me some tips for improvement I'd be grateful :)

    lib/database.php:
    <?php
    
    class database {
    
    	public static $connection;
    	
    	public function __construct($db_host = DB_HOST, $db_name = DB_NAME, $db_username = DB_USERNAME, $db_password = DB_PASSWORD) {
    		self::$connection = mysql_connect($db_host.':'.$db_port, $db_username, $db_password) or errors::write_error('MySQL Connection failure');
    		mysql_select_db($db_name, self::$connection) or errors::write_error('MySQL Database selection failure');
    	}
    
    }
    
    class query {
    	
    	public $sql;
    	public $success;
    	public $resource;
    
    	public function __construct($sql, $autoprefix = true, $autoexecute = true) {
    		$this->sql = $sql;
    		if($autoprefix === true) $this->prefix();
    		if($autoexecute === true) $this->execute();
    	}
    
    	public function prefix() {
    		$this->sql = str_replace("#", DB_TBLPREFIX, $this->sql);
    	}
    	
    	public function execute() {
    		$this->resource = mysql_query($this->sql, database::$connection) or errors::write_error(mysql_error(database::$connection));
    		$this->success = (is_resource($this->resource)) ? true : false;
    	}
    	
    }
    
    class insert_query extends query {
    	
    	protected $table;
    	protected $fields;
    	protected $values;
    	const RAW_DATA = 1;
    	
    	public function __construct($table, $fields, $values) {
    		if(count($fields) == count($values)) {
    			$this->table = $table;
    			$this->fields = implode(',', $fields);
    			foreach($values as $k => $value) {
    				if(is_array($value)) {
    					if(count($value) == 2 && $value[0] == self::RAW_DATA) {
    						$this->values .= $value[1].',';
    					}
    				}
    				else {
    					$this->values .= '\''.$value.'\',';
    				}
    			}
    			$this->values = substr($this->values, 0, (strlen($this->values) - 1));
    			return parent::__construct('INSERT INTO `'.$this->table.'` ('.$this->fields.') VALUES ('.$this->values.')');
    		}
    		else {
    			errors::write_error('Field count did not match value count');
    			return false;
    		}
    	}
    		
    }
    
    
    
    ?>
    Code (markup):
    index.php (example usage):
    
    <pre>
    <?php
    
    include '../config.php';
    include 'lib/errors.php';
    include 'lib/database.php';
    
    $database = new database();
    
    $query = new query("SELECT hello FROM #_world");
    
    while($names = mysql_fetch_array($query->resource)) {
    	echo $names['hello']."\n";
    }
    
    $insert = new insert_query('#_world', array('hello'), array('Will'));
    
    ?></pre>
    Code (markup):

     
    Will Morgan, Dec 4, 2007 IP