Learning PHP Object Oriented Programming Beyond the basic.
Note: This is intended for my personal use only just incase I will for get this things during my PHP Test. However, I you find this helpful to you, feel free to copy the code. This set of code is base from what I learned from lynda tutorial beyond the basic. The video has been really useful to me and learn alot of things from it.
//Config.php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "root");
defined('DB_PASS') ? null : define("DB_PASS", "admin");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
//Database.php
require_once("config.php");
class MysqlDatabase{
    private $connection;
    public $last_query;
    private $magic_qoutes_active;
    private $new_enough_php;
    function __construct(){
        $this->open_connection();
        $this->magic_quotes_active = get_magic_quotes_gpc();
    $this->new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
        
        }
    public function open_connection(){
        $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
        if(!$this->connection){
            die("Database connection failed:" .mysql_error());
            }
        else{
            $db_select = mysql_select_db(DB_NAME, $this->connection);
            if(!$db_select){
                die("Database selection failed:". msql_error());
                
                }
            
            }
        
        
        }
    public function close_connection(){
        if(isset($this->connection)){
            mysql_close($this->connection);
            unset($this->connection);
            
            
            }
        
        
        }
    public function query($sql){
        $this->last_query = $sql;
        $result = mysql_query($sql,$this->connection);
        
        $this->confirm_query($result);
        
        return $result;
    
        }
    //Database Neutral Methods
    
    
    public function insert_id(){
        return mysql_insert_id($this->connection);
        }
    public function fetch_array($result){
        return mysql_fetch_array($result);
        }
    public function affected_rows(){
        return mysql_affected_rows ($result);
        }
    public function num_rows($result){
        return mysql_num_rows($result);
        }
    
    private function confirm_query($result){
        
        if(!$result){
            
            $output = "Database query failed:". mysql_error()."
";
        //    $output .= "Last SQL Query: ".$this->last_query;
            die($output);
            }
        
        }    
    public function escape_value( $value ) {
    
    if( $this->new_enough_php ) { // PHP v4.3.0 or higher
        // undo any magic quote effects so mysql_real_escape_string can do the work
        if($this->magic_quotes_active ) { $value = stripslashes( $value ); }
        $value = mysql_real_escape_string( $value );
    } else { // before PHP v4.3.0
        // if magic quotes aren't already on then add slashes manually
        if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
        // if magic quotes are active, then the slashes already exist
    }
    return $value;
}
    
    }
$database = new MysqlDatabase();
$db = & $database;
?>
//Functions.php
function strip_zeros($marked_string=""){
    $no_zeros = str_replace('*0','',$marked_string);
    $clean_string = str_replace ('*','',$no_zeros);
    return $clean_string;
    
    }
function redirect_to($location = NULL){
    if($location != NULL){
        header ("Location: {$location}");
        exit;
    
        }
    
    }
    
function output_message($message=""){
    if(!empty($message)){
        return "
{$message}
";
    }
    else
    return "";
    }    
?>
//User.php
require_once('database.php');
class User{
    public $id;
    public $username;
    public $password;
    public $first_name;
    public $last_name;
    
    
    
    public static function find_all()
        {
        global $database;
        return self::find_by_sql("Select * FROM users");
        }
    public static function find_by_id($id=0)
        {
        global $database;
        $result_set = $database->query("SELECT * FROM users WHERE id={$id}");
        $found = $database->fetch_array($result_set);
        return $found;
        }
    public static function find_by_sql($sql=""){
        global $database;
        $result_set = $database->query($sql);
        return $result_set;
        
        }
    public function full_name(){
        if(isset($this->first_name) && isset($this->last_name)){
            return $this->first_name." ".$this->last_name;
            
            }
        }
    private static function instantiate($result){
        
        $object                 = new self;
        //$object->id             = $record['id'];
        //$object->username     = $record['username'];
        //$object->password     = $record['password'];
        //$object->first_name     = $record['first_name'];
        //$object->last_name     = $record['last_name'];
        
        
        foreach($record as $attribute=>$value){
            
        if($object->has_attribute($attribute)){
            $object->$attribute = $value;
            }
            }
        return $object;
        
        }
    private function has_attribute($attribute){
          // get_object_vars returns an associative array with all attributes 
      // (incl. private ones!) as the keys and their current values as the value
      $object_vars = get_object_vars($this);
      // We don't care about the value, we just want to know if the key exists
      // Will return true or false
      return array_key_exists($attribute, $object_vars);
        
        }
    }
?>
//Index.php
require_once("../includes/database.php");
require_once("../includes/user.php");
$record = User::find_by_id(1);
echo $user->username;
echo "
";
echo $user->full_name();
?>