Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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'";    
    $this->query($sql);
      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.

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

//search.php
  require_once('classes/search.class.php');  
  $search = new Search($_GET);
  $result = $search->search();
  echo $result; 


//search.class.php
include('classes/bll.class.php');

class Search extends BLL {
  private $store_term;

  public function __construct($term) {
    parent::__construct();
    $this->get_search_term($term);    
  }

private function get_search_term($term) {}

public function search() {
  if(!empty($this->store_term))
    $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;
}


//bll.class.php
class BLL extends DAL
{

  public function __construct() {
   parent::__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);
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $sth->execute(array(':term'=>$term));
    $result = $sth->fetchAll();    
    return $result;
  }  
}

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

share|improve this question
    
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 –  Martin Blore 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

2 Answers 2

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.

share|improve this answer
    
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
    
Beware of "Pattern Fever" if you read the Head First Design Patterns... –  Martin Blore Mar 25 '11 at 18:47
up vote 0 down vote accepted

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:
http://www.devshed.com/c/a/PHP/The-Dependency-Injection-Design-Pattern-in-PHP-5/1/
http://www.devshed.com/c/a/PHP/Applying-the-Dependency-Injection-Pattern-and-Factory-Methods-in-PHP-5/1/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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