I know there is oodles of threads on this and MVC but I just can't seem to get my head around it. I don't really want to go anywhere near a full MVC model but want to understand the basics of writing 'model' objects in order to separate my html from my php. A couple of qs:
1) A standard page setup:
->include global libs, classes, functions.
->construct a "handler" object to handle all the business logic and format result sets in data arrays - in order to consistently handle data in a foreach loop.
->include page header
->presentation code pulling from the "handler" methods and properties.
->include page footer
Anybody still with me? Is this a viable page setup for code separation.
2) My top level "handler_class" was starting to look like the following before I realized I didn't know what I was doing.
public function table_handler($database=DB_DATABASE,$username=DB_USERNAME,$password=DB_PASSWORD,$server=DB_SERVER){
$this->username = $username;
$this->password = $password;
$this->server = $server;
$this->database = $database;
$this->connect();
}
public function get_server(){
return $this->server;
}
public function get_username(){
return $this->username;
}
public function get_database(){
return $this->database;
}
public function get_con(){
return $this->con;
}
public function connect(){//Connects to mysql server and selects database
$con = mysql_connect($this->server,$this->username,$this->password);
$this->con = $con;
if(!mysql_query("USE ".$this->database))
die(mysql_error());
return $con;
}
private function mysql_qry($qry){
if(!$result=mysql_query($qry)){
die(mysql_error());
}
return $result;
}
public function qry($qry){//Executes mysql query
unset($this->data_array);
$result = $this->mysql_qry($qry);
$this->data_array = $this->to_data_array($result);
return $this->data_array;
}
public function get_all_data($where){//Executes mysql query
unset($this->data_array);
$result = $this->mysql_qry("SELECT * FROM ".$this->table." WHERE ".$where);
$this->data_array = $this->to_data_array($result);
return $this->data_array;
}
private function to_data_array($result){
$index_pt=0;
$data_array;
while($record = mysql_fetch_assoc($result)){
foreach($record AS $key=>$value){
echo $key.":".$value."<br />";
$data_array[$index_pt][$key]=$value;
}
$index_pt++;
}
return $data_array;
}
}
Any comments welcome or even simple page layouts/methodologies which start to resemble efficient code separation.
What you describes seems more than reasonable, although I personally would recommend investing the time in properly learning MVC. Personally I don't write any sizeable application using any other method these days, it's just such a convenient way to work separating data management, business logic and presentation.
I would suggest using one of the OOP database extensions, such as MySQLi or PDO instead of the old MySQL extension. Not only does it allow your code to be more truly object-oriented, but instead of writing a complete database handler you can just extend an existing class to add your additional functionality. For example:
PHP Code:
<?php /** * Database wrapper **/ class Handler extends mysqli { public function __construct($host, $user, $pass, $database) { parent::__construct($host, $user, $pass, $database); $result = mysqli_connect_error(); if ($result != '') { throw new Exception($result); } }
public function qry($sql) { $result = $this->query($sql); if (is_object($result)) { return $this->to_data_array($result); } return $result; }
private function to_data_array($result) { $data_array = array(); while ($record = $result->fetch_assoc()) { $data_array[] = $record; } return $data_array; } }
/** * Sample model class for User table **/ class User { private $db; private $data = array();
public function __construct(Handler $db) // type-hinting is a powerful PHP5 tool :) { $this->db = $db; }
public function populate($id) { $sql = "SELECT * FROM user WHERE id = " . (int)$id; $result = $this->db->qry($sql); if (is_array($result) and count($result) == 1) { $this->data = $result; return true; } return false; }
public function getData() { return($this->data); }
// other methods for updating user, deleting user, etc.... }
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks