A collection of cool website available in 2012.
PHP Abstract Class
- 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(); }
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; } }
abstract class querybuilder { abstract function fetch() { return false; } // php akan return error kerana abstract method sepatutnya dibiarkan kosong }
Langgan:
Catatan (Atom)