 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
11-12-2007, 12:53 PM
|
#21 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
|
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.
|
|
|
11-13-2007, 09:55 AM
|
#22 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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)
|
|
|
|
11-14-2007, 02:28 PM
|
#23 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
11-23-2007, 04:17 PM
|
#24 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Nice article, I learned a bunch.
For those who want to query multiple databases within the same page. Comment out 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($type, self::$_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. :)
|
|
|
|
04-05-2008, 08:57 PM
|
#25 (permalink)
|
|
The Visitor
Join Date: Apr 2008
Posts: 3
Thanks: 0
|
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 ?
|
|
|
|
04-05-2008, 09:32 PM
|
#26 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
04-05-2008, 09:58 PM
|
#27 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,324
Thanks: 5
|
Another alternative would be to stop repeating yourself 100 times and only write the database connection line once.
|
|
|
|
04-06-2008, 12:38 PM
|
#28 (permalink)
|
|
The Visitor
Join Date: Apr 2008
Posts: 3
Thanks: 0
|
Quote:
Originally Posted by Salathe
Another alternative would be to stop repeating yourself 100 times and only write the database connection line once.
|
how you do that ?
|
|
|
|
04-06-2008, 12:57 PM
|
#29 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,324
Thanks: 5
|
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).
|
|
|
|
04-06-2008, 01:07 PM
|
#30 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Salathe
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
|
|
|
|
04-06-2008, 01:16 PM
|
#31 (permalink)
|
|
The Visitor
Join Date: Apr 2008
Posts: 3
Thanks: 0
|
Quote:
Originally Posted by Salathe
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 ?
|
|
|
|
02-04-2009, 11:06 AM
|
#32 (permalink)
|
|
The Visitor
Join Date: Feb 2009
Posts: 2
Thanks: 0
|
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?
|
|
|
|
02-04-2009, 01:32 PM
|
#33 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
|
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.
|
|
|
02-04-2009, 09:59 PM
|
#34 (permalink)
|
|
The Visitor
Join Date: Feb 2009
Posts: 2
Thanks: 0
|
Excellent, thanks!
|
|
|
|
09-17-2009, 02:57 AM
|
#35 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 73
Thanks: 30
|
Thanks for good tutorial.
|
|
|
|
09-25-2009, 12:05 PM
|
#36 (permalink)
|
|
That guy
Join Date: Sep 2009
Location: San Antonio, TX
Posts: 24
Thanks: 0
|
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!
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|