I’m going to talk about some good object oriented practices for small/medium projects. The object oriented programming itself will save you enourmous amount of time. The inheritance is part of the Object Oriented Programming (OOP) and is very good thing that is going to save you even more time when you start to use some object oriented model in your programming.
Let’s say we need to have some root class – lets say “Base”, with the __construct() (the class’s constructor) – notice that since PHP 5.0 the constructor of a class is defined in __constructs(), instead of “the-name-of-your-class”(). Anyway you can define the constructor information in Base() as well, but better do it in __construct, because you don’t know if there will be support for the old PHP 4 constructors in PHP 6
so let’s get on business..
In our Base class we are going to add some protected variables (they are going to be viewed from all delivered classes, and from the Base itself)
In our case we add a constructor that creates a mysqli connection to our host, and that’s all we are going to use this class for – for the connection with the DataBase.
Class Base {
protected $host = “localhost”;
protected $user = “root”;
protected $pass = “”;
protected $name = “MerchantsDB;
protected $char = “utf-8″;
protected $table = “merchants”;
function __construct(){
if(($this->link = new mysqli($this->host, $this->user, $this->pass, $this->name)) === false){ $this->error(“cant login”);
}else{
//echo “logged
“;
$this->link->set_charset($this->char) or $this->error(“set charset”);
}
}
}
Class Merchant extends Base {
function AddMerchant($mName, $mURL, $mAgent, $mUID, $mDate, $mManager, $mStatus, $mComment){
$q = mysqli_query($this->link, “INSERT INTO “.$this->table.” (`MerchantName`, `MerchantURL`, `MerchantAgent`, `MerchantUserID`, `MerchantReceivedDate`, `MerchantManager`, `MerchantStatus`, `MerchantComment`) VALUES (‘$mName’, ‘$mURL’, ‘$mAgent’, ‘$mUID’, ‘$mDate’, ‘$mManager’, ‘$mStatus’, ‘$mComment’)”);
if($q){
echo “Merchant Successfully added !!”;
return true;
}else{
echo “Invalid input data or duplicated URL !!”;
return false;
}
}
function RemoveByID($id){
if(mysqli_query($this->link,”Delete from “.$this->table.” where MerchantID = “.$id)){
//echo “entry $id successfully removed”;
}
}
}
function GetAll($order){
$q = mysqli_query($this->link,”select * from “.$this->table.” ORDER BY “.$order.” desc”);
$count = 1;
while($row = mysqli_fetch_assoc($q)){
include(“Merchants.table.inc.php”);
$count++;
}
}
}
So we have a class Merchant that is delivered from Base and so we inherit $link variable from the Base’s constructor. So when we call $this->link we have a fully working mysqli connection (that is more secure than the standard mysql).
As you see we have created 3 very simple functions of the Merchant class GetAll, RemoveByID, and AddMerchant
in all of them we use $this->link delivered from our base class, which is very cool, because we can have a bunch of other classes – like Product, Car, Student… whatever and we don’t have to specify a single connection for each of them – we just inherit it from the Base class – with “extends Base” at the class declaration
that’s it for now i hope this example was useful for you
June 16th, 2010 at 11:35 am
My English is not good, but to see the article you write a good feel of your