TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (4) Thread Tools Search this Thread Display Modes
Old 11-12-2007, 12:53 PM   #21 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
Wildhoney is on a distinguished road
Default

We finally got the pupils up to scratch? :)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-13-2007, 09:55 AM   #22 (permalink)
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

Haha, yea (A)

Sketch is nice :D

Actually, I love this place, cause ppl are so helpful. On other PHP/Webcoding communities people are stupid, and tells everyone to go away if you don't know anything.

Really hope more people starts posting here, so we can get a big community(and you can make lots of money to spend on the site :D)
Tanax is offline  
Reply With Quote
Old 11-14-2007, 02:28 PM   #23 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
We finally got the pupils up to scratch?
hahaha, yea hopefully.

I must say it help me further embed the technology into my head, where i would usually forget it :)

And i agree with you Tanax this forum is full of knowlegable and nice people who wont label you a noob when you, (ask as far as they are concerned)ask a seemingly simple question, i know what it feels like to read a tutorial 3 or 4 times and still end up goin 'WHAAAAAAAAAAT THEEEEEE ****'.

I hope talkPHP will continue to grow (im sure it will) and be the best PHP forum for help and tutorials.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-23-2007, 04:17 PM   #24 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Nice article, I learned a bunch.

For those who want to query multiple databases within the same page. Comment out
PHP Code:
$pDB 
and where it checks if $pDB is an object or not. Or else, since $pDB is static, you will only be able to instantiate one database. :)

Also, I'm not quite done modifying my database factory. But I do think it'd be neat to implement something like this:
PHP Code:
public static $_dbTypes = array(
        
'mysql' => 'MySQL',
        
'odbc' => 'ODBC'
    
);

...

    public static function 
factory($type) {
        
//if(!is_object(self::$_db)) {
            
if(array_key_exists($typeself::$_dbTypes)) {
                echo 
$type ' is a supported database!<br />';
                
self::$_db = new self::$_dbTypes[$type];
            } else {
                throw new 
Exception($type ' is not a supported database!');
            }
        
//}
        
        
return self::$_db;
    } 
It's not perfect and yes, $_db is static, but it's work in progress. :)
bdm is offline  
Reply With Quote
Old 04-05-2008, 08:57 PM   #25 (permalink)
The Visitor
 
Gurzi's Avatar
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Gurzi is on a distinguished road
Default

Hello guys,

this is my first post and I'm not an native english speaker so i will try to do my best.
I'm not seeing the right purposes of using factory pattern. You are trying to hide the specific type of the object when instantiating it. But you have to specify which type of database you want to use.
You made something like this :
$factory->connect('mysql);

so, now imagine that you are connecting with this method in 100 different pages. and now you want to change from mysql to oracle.
You need to open all the 100 files and change the $factory->connect('mysql') to $factory->connect('oracle') :s

or am i wrong ?
Gurzi is offline  
Reply With Quote
Old 04-05-2008, 09:32 PM   #26 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

hi m8,

I can see where you are coming from but there are a few ways to solve this problem, one of them being having a central config with a define() maybe, like so:
config.php
PHP Code:
//config
define('DB_TYPE''mysql'); //or oracle 
then the call would be:
PHP Code:
$factory->connect(DB_TYPE); 
the other alternitive is to maybe use the registry pattern, but thats a article for another day.

hope this helps :)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 04-05-2008, 09:58 PM   #27 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,324
Thanks: 5
Salathe is on a distinguished road
Default

Another alternative would be to stop repeating yourself 100 times and only write the database connection line once.
Salathe is offline  
Reply With Quote
Old 04-06-2008, 12:38 PM   #28 (permalink)
The Visitor
 
Gurzi's Avatar
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Gurzi is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Another alternative would be to stop repeating yourself 100 times and only write the database connection line once.

how you do that ?
Gurzi is offline  
Reply With Quote
Old 04-06-2008, 12:57 PM   #29 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,324
Thanks: 5
Salathe is on a distinguished road
Default

There is no single solution. You could include a file which has the database connection code, for one simple solution. If you're writing the same line of code 100 times (or even 50, 10, 5 times!) then you're setting yourself up for problems in the future as has already been recognised (the example of having to edit 100 files just to change the database type).
Salathe is offline  
Reply With Quote
Old 04-06-2008, 01:07 PM   #30 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
There is no single solution. You could include a file which has the database connection code, for one simple solution. If you're writing the same line of code 100 times (or even 50, 10, 5 times!) then you're setting yourself up for problems in the future as has already been recognised (the example of having to edit 100 files just to change the database type).
Yeah, that's my problem, I love to code so much that I write more code that is useless. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 04-06-2008, 01:16 PM   #31 (permalink)
The Visitor
 
Gurzi's Avatar
 
Join Date: Apr 2008
Posts: 3
Thanks: 0
Gurzi is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
There is no single solution. You could include a file which has the database connection code, for one simple solution. If you're writing the same line of code 100 times (or even 50, 10, 5 times!) then you're setting yourself up for problems in the future as has already been recognised (the example of having to edit 100 files just to change the database type).

If you use singleton and you include the database class that opens the connection in multiple different pages i saw that the object is the SAME. how is that possible ( when you define something static this pass through multiple pages ?)

I'm saying this because i used spl_object_hash and it returned the same hash in different pages.

i just included the class with the static object (using singleton).

is this normal ?
Gurzi is offline  
Reply With Quote
Old 02-04-2009, 11:06 AM   #32 (permalink)
The Visitor
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
asciant is on a distinguished road
Default

Thanks again for a great article, sorry to rehash old banter, but I have a quick question (probably more some clarification).

Based on the singleton theory if I were to call DBfactory::factory()->query(".."); from anywhere in my application, it would be referring to the one single instance that exists?

So if I'm following correctly, I could essentially call this from anywhere in my application (or any other class) in a similar fashion to a global variable?
asciant is offline  
Reply With Quote
Old 02-04-2009, 01:32 PM   #33 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
Wildhoney is on a distinguished road
Default

Yes, you understand it well, Asciant. Once the class is loaded on the page you require, it'll be accessible from any scope, but will only use the one instance of the object. Ever.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-04-2009, 09:59 PM   #34 (permalink)
The Visitor
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
asciant is on a distinguished road
Default

Excellent, thanks!
asciant is offline  
Reply With Quote
Old 09-17-2009, 02:57 AM   #35 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

Thanks for good tutorial.
hello-world is offline  
Reply With Quote
Old 09-25-2009, 12:05 PM   #36 (permalink)
That guy
 
cachepl0x's Avatar
 
Join Date: Sep 2009
Location: San Antonio, TX
Posts: 24
Thanks: 0
cachepl0x is on a distinguished road
Default

I really quite enjoy this thread. It offers a lot of information for the new-comers, and a great method for switching database types. Great job, Sketch!
cachepl0x is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1321-using-factory-pattern-mad-rantings-mind-without-coffee.html
Posted By For Type Date
PHP Advanced PHP: Using the Factory Pattern Tutorial This thread Refback 01-12-2008 02:40 PM
Using the factory pattern (mad rantings of a mind without coffee) - TalkPHP This thread Refback 12-29-2007 10:41 PM
PHP OOP Advanced PHP: Using the Factory Pattern Tutorial This thread Refback 12-22-2007 04:50 AM
del.icio.us/network/mahatm This thread Refback 12-21-2007 09:38 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:45 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design