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

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
joseazr1987
Forum Newbie
Posts: 10
Joined: Fri Aug 27, 2010 11:30 am

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

Post 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??..
User avatar
joseazr1987
Forum Newbie
Posts: 10
Joined: Fri Aug 27, 2010 11:30 am

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

Post 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?
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

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

Post 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
User avatar
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)

Post 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;
}
(#10850)
Post Reply