Page 1 of 1

Work on Layers on Php (User, Busines, Data)

Posted: Mon Sep 13, 2010 12:14 pm
by joseazr1987
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??..

Re: Work on Layers on Php (User, Busines, Data)

Posted: Tue Sep 21, 2010 1:17 pm
by joseazr1987
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

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;
    }
}
This is my Object Businer

Code: Select all


include('db.class.php);

class Car extends db{
     
     function Cars(){
	$sql = 'SELECT * FROM `cars`';
	return $this->fetch_assoc($sql);
    }
}
and finally this is the view of the user

Code: Select all

include('car.class.php');
$objCar = new Car();

$cars = $objCar->Cars();

foreach($cars as $car){
     echo $car;
}
So.. what do you think? can be working on layer? or not?

Re: Work on Layers on Php (User, Busines, Data)

Posted: Sat Oct 09, 2010 2:31 am
by kalpesh.mahida
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

Re: Work on Layers on Php (User, Busines, Data)

Posted: Sat Oct 09, 2010 11:57 am
by Christopher
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;
}