first php framework

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
Ok so it's my first time developing a small framework for myself, just to make life a little easier, I have created an Abstraction Layer for the database, however I am not entirely sure how I can shorten some functionality within it. Maybe you could help me,

I have a configuration file which stores all defines such as the database connectivity.

I have a registry which deals with storing objects from an object folder (database class) which is pre instantiated upon the execution of a command called - $registry::ADD_OBJECT('db'); this will then instantiate my db class so I can make references to functions inside of that like so:

$registry::GET_OBJECT('db')->query('users');
$registry::GET_OBJECT('db')->rows(5); // display the first 5 results.

however is there any way I can perhaps shorten the initial part down, by loosing the $registry? or even combining the $registry with GET_OBJECT. like

rGET_OBJECT->query('users');

any help would be great, theres no point giving the source as the file amount of lines is crazy, however any methods you can advise on would be brilliant.
 
You could shorten it but to be honest I wouldn't. Leaving it like it is it keeps things descriptive and will make it easier to see what's going on when you come back to the code in the future. I'm frequently using code like:

PHP:
CachingObjectFactory::get('DatabaseFactory')->getConnection('foo')->query($query);
 
ahh brilliant, ok.
I love the fact that the framework has cut my production time by an absolute load so far, such a handy app, first one i've made which actually works :p

Thanks anyway
 
I use a much simpler method.

Have the database class instantiate itself upon first use by implementing it as a singleton with static functions throughout.

e.g.

DB::get_row("SELECT * FROM Product Where idProduct = 1");

in DB::get_row() function you first check to see if there's an active connection. If not then you initialise the connection. Then you do the normal query stuff and return the results.

Subsequent calls to DB::get_row or similar functions which rely on a connection will use the existing connection.
 
You could shorten it but to be honest I wouldn't. Leaving it like it is it keeps things descriptive and will make it easier to see what's going on when you come back to the code in the future. I'm frequently using code like:

PHP:
CachingObjectFactory::get('DatabaseFactory')->getConnection('foo')->query($query);

That's utterly over-engineered. What's the need for two levels of factories? What's wrong with this?

PHP:
$connection = new DatabaseConnection($dbName, $userName, $password);
// ...
$connection->query($sql);

If you need global access, then put a static method on either DatabaseConnection or on some core class that provides global resources. Of course, if you're following good inversion of control principles, then you'd try not to provide global access in this way.

If you only need one connection, then you could make it even more concise by making all of the methods static:

PHP:
Database::connect($dbName, $userName, $password);
Database::query($query);
 
Last edited:
I won't go into detail but DatabaseFactory has a lot of extra connection creation related code which fits better in a separate class rather than bundle it in with the generic object factory. We have a lot of connections to different databases so we can't use static methods. To avoid globals/singletons the 'caching' part of the CachingObjectFactory stores objects which can be reused, unless otherwise requested, when get() is called.
 
I won't go into detail but DatabaseFactory has a lot of extra connection creation related code which fits better in a separate class rather than bundle it in with the generic object factory. We have a lot of connections to different databases so we can't use static methods. To avoid globals/singletons the 'caching' part of the CachingObjectFactory stores objects which can be reused, unless otherwise requested, when get() is called.

How is this different to something like (excuse my ignorance btw :) ):

Code:
public static function getInstance() {

if (!self::$instance)
    {
    self::$instance = new PDO("mysql:host=localhost;dbname=periodic_table", 'username', 'password');;
    self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
return self::$instance;
}

//useage
$row = $db::getInstance()->query(.........
 
Back
Top Bottom