01-23-2008, 10:28 AM
|
#11 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
Wow, really?
You're learning fast
Here's a more advanced example, taken from another thread:
PHP Code:
class DBfactory
{
public static $pDB;
public static function &factory($szType = "")
{
if(!is_object(self::$pDB))
{
switch($szType)
{
case 'mysql':
self::$pDB = new DBmysql;
break;
case 'mssql':
self::$pDB = new DBmssql;
break;
default:
self::$pDB = new DBmysql;
break;
}
}
return self::$pDB;
}
}
And to use it:
PHP Code:
$db = DBfactory::factory("mysql");
$db->query("..");
If the DBfactory wouldn't have static function and variables, you would have to declare the object first:
PHP Code:
$DBfactory = new DBfactory();
$db = $DBfactory('mysql');
And then we can use the functions of the db object such as for example executing a query.
So the advantage here is that you don't have to create the object DBfactory since it's static, and in the actual static function, it create the object.. and returns it.
Ofcourse you have to include the files containing the classes of the objects you want to create. ANd this can be used not just for DB. I'm using it as a ALLround system.
PHP Code:
$db = DB::getInstance('DBmysql');
TANAXIA::setDB($db);
$news = TANAXIA::getInstance('news');
$forum = TANAXIA::getInstance('forum');
And so on..
I have 2 factory classes there as you can see.
One for the DB, and it contains the creation of objects of
DBmysql
DBmysqli
DBoracle
etcetc..
And TANAXIA contains the creation of objects for misc things using the DB class.
Using the factory pattern (mad rantings of a mind without coffee)
|
Thanks! Since I'm basically asleep here, it's kinda hard for me to concentrate, the reason I'm up is cause code doesn't bore me like other people :P but umm, it's 5:26 AM >.<
As stated in my Profile, I'm addicted to and I'm in love with Computers, true nerd here xD
__________________
VillageIdiot can have my babbies ;d
|
|
|
|