This is my first "large scale" system I've designed for myself. Since I'm more of a front end developer than backend developer, I'm not sure if my architecture is sound.
I've created a Factory
class which I use to create all my classes and connect to the DB. I think I've tried to use used Singelton(?) pattern, but I'm not really sure.
It probably would be easier if you knew more about the system, but in short it's an online directory mixed with user interaction and social media.
It would be great if I could get some feedback on this.
My factory class
include_once('abc.class.php');
class Factory {
// One function for each class I include
// This is the only place I include class files
function new_abc_obj($id = NULL) { return new Abc(Conn::get_conn(), $id); }
}
My DB connection class
class Conn {
private static $conn = NULL;
/* Establish PDO DB connection */
private static function init() {
$conf = self::config();
try {
self::$conn = new PDO($conf['dsn'], $conf['user'], $conf['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch (PDOException $e) {
// We remove the username if we get [1045] Access denied
if (preg_match("/\b1045\b/i", $e->getMessage()))
echo "SQLSTATE[28000] [1045] Access denied for user 'name removed' @ 'localhost' (using password: YES)";
else
echo $e->getMessage();
}
}
/* Get DB connection */
public static function get_conn() {
if (!self::$conn) { self::init(); }
return self::$conn;
}
/* Get username, password and db data from config.php */
private static function config() {
$conf = array();
$conf['user'] = DB_USER;
$conf['pass'] = DB_PASSWORD;
$conf['dsn'] = 'mysql:dbname='.DB_NAME.';host='.DB_HOST;
return $conf;
}
}
My class interface
interface iAbc {
public function one();
public function two();
}
My Business Logic Layer (BLL)
class Abc extends AbcDAO implements iAbc {
private $data = array();
protected $db;
function __construct(&$db, $id) {
$this->db = &$db;
if($id) {
$this->setData($id);
}
}
// __set, __get, __isset is here
public function one(){};
public function two(){
$result = parent::get_some_data();
return $result;
};
}
My Data Access Layer (DLL)
class AbcDAO {
protected function get_some_data($name){
$sql ="SELECT 123;";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
}
}
And this is how I create the object:
$my_abc = Factory::new_abc_obj();
$test = $my_abc->two();