Hi, I'm new in this for the programing, I want learn about the Architecture Desing for programing, and I want to start to program on layers (User, Busines, Data)..
some one have a example for this??..
Work on Layers on Php (User, Busines, Data)
Moderator: General Moderators
- joseazr1987
- Forum Newbie
- Posts: 10
- Joined: Fri Aug 27, 2010 11:30 am
- joseazr1987
- Forum Newbie
- Posts: 10
- Joined: Fri Aug 27, 2010 11:30 am
Re: Work on Layers on Php (User, Busines, Data)
Well .. I will share with you my way of working, I do not think it is in layers, but split in three parts, one with the functions mysql_fetch_assc, mysql_query, etc.
another do the necessary operations, and elsewhere I show the user
This is my Object Data
This is my Object Businer
and finally this is the view of the user
So.. what do you think? can be working on layer? or not?
another do the necessary operations, and elsewhere I show the user
This is my Object Data
Code: Select all
mysql_connect(HOST,USER,PASS);
mysql_select_db(NAME);
class db{
function fetch_assoc($res){
$row = mysql_fetch_assoc($res);
return $row;
}
}
Code: Select all
include('db.class.php);
class Car extends db{
function Cars(){
$sql = 'SELECT * FROM `cars`';
return $this->fetch_assoc($sql);
}
}
Code: Select all
include('car.class.php');
$objCar = new Car();
$cars = $objCar->Cars();
foreach($cars as $car){
echo $car;
}
-
kalpesh.mahida
- Forum Commoner
- Posts: 36
- Joined: Wed Oct 06, 2010 7:09 am
Re: Work on Layers on Php (User, Busines, Data)
What you are doing is fine but still you can improve it and make it more structured. Given bellow few hints and list of resources which can help you to write more structured PHP applications.
If you want to architect your application in such a way where your business logic and presentation part resides separate then you can achieve that by using some template engine or MVC frameworks in your application development (also you can build your own), which allows great separation between business logic and presentation.
Template Engines
http://www.smarty.net/
http://dwoo.org/
http://phpsavant.com/
MVC Frameworks
Zend Framework: http://framework.zend.com/
CakePHP: http://cakephp.org/
Codeigniter: http://codeigniter.com/
Symfony: http://www.symfony-project.org/
Hope this will help you.
Kalpesh Mahida
If you want to architect your application in such a way where your business logic and presentation part resides separate then you can achieve that by using some template engine or MVC frameworks in your application development (also you can build your own), which allows great separation between business logic and presentation.
Template Engines
http://www.smarty.net/
http://dwoo.org/
http://phpsavant.com/
MVC Frameworks
Zend Framework: http://framework.zend.com/
CakePHP: http://cakephp.org/
Codeigniter: http://codeigniter.com/
Symfony: http://www.symfony-project.org/
Hope this will help you.
Kalpesh Mahida
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Work on Layers on Php (User, Busines, Data)
I think I might compose the objects:
Code: Select all
class Db{
protected $link;
protected $result;
function __construct($host, $user, $pass, $name){
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($name, $this->link);
}
function query($sql){
$this->result = mysql_query($sql, $this->link);
}
function fetch_assoc(){
$row = mysql_fetch_assoc($this->result);
return $row;
}
}
Code: Select all
class Car {
function __construct($db){
$this->db = $db
}
function findAll(){
$sql = 'SELECT * FROM `cars`';
$this->db->query($sql);
$rows = array();
while($row = $this->db->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
}
Code: Select all
$db = new Db(HOST, USER, PASS, NAME);
$objCar = new Car($db);
$cars = $objCar->findAll();
foreach($cars as $car){
echo $car;
}
(#10850)