- Matlamat Abstract Class adalah untuk di-extend oleh Class lain. Jika kita try instantiated class tersebut dengan new keyword, php akan keluar "Cannot instantiate abstract class". Oleh itu meletakkan function constructor di dalam abstract kelas adalah satu perbuatan yang sia-sia. PHP tidak akan raise error mahupun warning.
abstract class querybuilder {
var $whereSQL;
var $innerjoin;
var $select;
var $join;
var $groupby;
var $extrafield;
function __construct() { /*pointless constructor*/ }
function where($id,$separator,$value) {
if (strtoupper($separator) == "LIKE") {
$this->whereSQL=$this->whereSQL." AND $id LIKE '$value' ";
}
else if (strtoupper($separator) == "NOT LIKE") {
$this->whereSQL=$this->whereSQL." AND $id NOT LIKE '$value' ";
}
else if ($separator == "=") {
$this->whereSQL=$this->whereSQL." AND $id = $value ";
}
else {
$this->whereSQL=$this->whereSQL." AND $id $value ";
}
}
function join($sql) {
$this->join=$this->join.$sql;
}
function groupby($sql) {
$this->groupby=$sql;
}
function extrafield($sql) {
$this->extrafield=$this->extrafield." ,".$sql;
}
abstract function compose($perPage,$page);
abstract function fetch();
abstract function count();
}
Sejak abstract class kita tidak boleh nak instiantiate, kita hanya boleh create satu class lain yang EXTEND abstract class. Di child class, kita boleh override method yang ada di dalam abstract class. Jika taknak overide method tersebut, pakai jer lah method tanpa ragu2x.
class query2db extends querybuilder{
var $table;
var $jaws;
var $returnAsArray;
function __construct($db,$table) {
/* contstructor untuk querydb class */
$this->table=$table;
$this->db=$db;
$this->returnAsArray=FALSE;
}
function returnAsArray(){
$this->returnAsArray=TRUE;
}
function compose($perPage,$page) {
/* overide abstract method. Defining my own set of rules */
if (($page=="")||($page==0)){
$page=1;
}
$start=($page*$perPage)-$perPage;
$db=$this->db;
$sql="SELECT `master` . * $this->extrafield
FROM $this->table AS `master` $this->join
WHERE 1 $this->whereSQL
$this->groupby
LIMIT $start,$perPage";
$this->jaws=$db->qr($sql,$this->returnAsArray);
$total = count($this->jaws);
return $total;
}
function count() {
/* overide abstract method. Defining my own set of rules */
$db=$this->db;
$sql="SELECT count(*) as `count`
FROM `$this->table` as `master` WHERE 1
$this->whereSQL";
$var = $db->qr($sql);
foreach ($var as $temp) {
$total=$temp->count;
}
return $total;
}
function fetch() {
/* overide abstract method. Define my own set of rules */
return $this->jaws;
}
}
Dalam kes ini, jika class query2db extends abstract class bernama querybuilder, di class query2b, saya harus create my method on count(), compose() dan fetch(). Ini adalah kerana, ketiga-tiga method tersebut di querybuilder class berstatus abstract. Jika kita tidak overide method ini, PHP kompiler akan menghadiahkan kita "Class query2db contains 1 abstract method and must therefore be declared abstract or implement the remaining methods".
Ini adalah kerana, abstract method ialah function yang kosong dan kita kene overide method tu dengan psuedo-kod kita. Jika kita cuba untuk membuat sesuatu di abstract method di dalam abstract class, dia akan return error "Abstract function querybuilder::compose() cannot contain body"
abstract class querybuilder {
abstract function fetch() {
return false;
}
// php akan return error kerana abstract method sepatutnya dibiarkan kosong
}
Tiada ulasan:
Catat Ulasan
Please comment, if you think it will help us all.