Thread: uhh... OOP?
View Single Post
Old 01-23-2008, 10:20 AM   #10 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Thanks for clearly that up for me, I've only been doing PHP for about 8 months to a year now so..

Off Topic:
GTalk notification of a new Email on the GMail email system has a long delay, lol.

By the way, you could just use the __autoload() magic method for grabbing php classes, :) though it doesnt work in the CLI
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)
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
Orc (01-23-2008)