I am learning some OOP on PHP and I have developed some sample code to practise.
Ignoring the functionality, I just want to learn the "best" or "better" way to write OOP codes in PHP.
the following is my code and I hope anyone who has been doing PHP can help to review or comment for any improvements or weaknesses on my code.
<?php
require_once "sys_config.php";
require_once "general.inc";
$para[":user"]= "%";
$dataset = UserFactory::getUsers($para);
var_dump($dataset);
?>
next is the UserFactory class
<?php
class UserFactory{
public static function getUsers($para)
{
$db = new UserDB("CALL getUsers(:user)",$para); //sp
$objs = $db->execute();
if(!empty($objs))
foreach( $objs as $obj)
{
$users[] = new user($obj);
}
return $users;
}
}
?>
next is my UserDB class
<?php
Class UserDB extends Database{
protected $conn;
protected $SQL;
protected $para;
function __construct($SQL, $para)
{
parent::__construct();
$this->conn = parent::getConn();
$this->SQL = $SQL;
$this->para = $para;
}
function execute()
{
$query = $this->conn->prepare($this->SQL);
if(!empty($this->para))
foreach($this->para as $key=>$val)
{
$query->bindParam($key,$val);
}
$results = $query->execute();
$dataset = null;
if($results)
{
while($row = $query->fetch(PDO::FETCH_OBJ))
{
$dataset[]=$row;
}
}
unset($this->conn);
parent::__destruct();
return $dataset;
}
}
?>
Next is my Database abstract class.
<?php
abstract Class Database{
protected $conn;
function __construct()
{
$this->conn = new PDO ("mysql:host=".DB_LOCATION.";dbname=".DB_NAME,DB_USER,DB_PASS);
}
function __destruct()
{
unset($this->conn);
}
function getConn()
{
return $this->conn;
}
abstract function execute();
}
?>
Last is my User object class
<?php
class User{
private $fullname;
private $id;
function __construct($userObj)
{
$this->fullname = $userObj->fullName;
$this->id = $userObj->id;
}
function setFullname($fullname)
{
$this->fullname = $fullname;
}
function getFullname()
{
return $this->fullname;
}
function setId($id)
{
$this->id = $id;
}
function getId()
{
return $this->id;
}
}
?>