I'm currently refactoring my DLL and BLL and I need some advice on what is the best way to work with DAL and BLL.

My current solution is like this:

// DAL class only consists of SQL queries and returns SQL result
class DAL {

  public function isBrandInList($brand)
    $sql ="SELECT id FROM sl_label  WHERE name = '$brand'";    
      return $this->query_result; 


// BLL will process data from DAL and return data to whatever function called the BLL function.
class BLL extends DAL {

  public function isBrandInList($name)
    $result = parent::isBrandInList($name);

    if(mysql_num_rows($result) > 0) {   
      while ($row = mysql_fetch_array($result)) {
        return $row['status'];
    } else { 
      return false;

I don't think this is the best way to work and my DAL file is getting over 1000 lines of code and has over 50 different classes. It's getting messy.

My plan is to improve this and start using MySQLi / PDO and prepared statements.

Can anyone tell me how I can improve the way I write my DAL and BLL? Should I create sub class in the DAL, e.g. one subclass for Person and one sub class for Brand?

I've googled and looked at diffrent design patterns. Most of them are for .NET and they don't have any good examples.

And maybe som links to some good articles? The ones I've found hasn't been the best.

This is an example on how I do it today. The code below is just snippets and not the entire code.

  $search = new Search($_GET);
  $result = $search->search();
  echo $result; 


class Search extends BLL {
  private $store_term;

  public function __construct($term) {

private function get_search_term($term) {}

public function search() {
    $result = $this->search_store();  

  return $result;

private function search_store() { 
  $result = parent::getStoresBySearch($this->store_term);
  $html = 'Code for building html search result list here';
  return $html;

class BLL extends DAL

  public function __construct() {

  public function getBrandsBySearch($term) {
    $result = parent::getBrandsBySearch(mysql_real_escape_string($term));
    return $result;

// dal.class.php
class DAL extends pdo {
  public function __construct($cfg_file = 'sl.config') {
    parent::__construct($this->dsn, $this->user, $this->pass);

  public function getStoresBySearch($term)
    $term = $term.'%';
    $sql = "SELECT bla bla bla";
    $sth = parent::prepare($sql);
    $result = $sth->fetchAll();    
    return $result;

I have a fealing that this might not be the best way to code?

  • Go read this (scroll down to about half way where it says "Using patterns to build a robust 3-tier architecture": dofactory.com/Framework/Framework.aspx Mar 25 '11 at 11:39
  • I have a general knowledge regarding 3-tier. It's kind of what I use now. Would e.g. class Store {} be considered a BL class?
    – Steven
    Mar 25 '11 at 16:32

After a lot of googling, I think I've finally found the solution I was looking for. I will be using Dependency Injection Design Pattern

Here are two links to a series of examples using DI:


The Head First series book on OOP should help; it's not .NET. Design seems more of a concern for you than language implementation.

This is how many developers and some projects tend to start out. You're in a much better position to sit back and reorganize your code. The key is not to lose your current functionality (Assuming it is working to your requirements.). You're about to find out why many prefer unit testing.

  • Yes. coding language is irrelevant. I would love to see some coding examples. The once I've found has been to basic. My current site is launched (Beta), and now I needn to improve code - hence refactoring. I know about unit testing and such. I just ned to work more structured on using DAL and BLL. I've added som example on how I've structured my code.
    – Steven
    Mar 25 '11 at 16:56
  • 1
    Beware of "Pattern Fever" if you read the Head First Design Patterns... Mar 25 '11 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.