note 91879 deleted from language.oop5.patterns by danbrown
| From: | danbrown@php.net | Date: | Tue, 30 Jun 2009 21:52:13 +0000 |
| Subject: | note 91879 deleted from language.oop5.patterns by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157725@lists.php.net to get a copy of this message | ||
Note Submitter: erik+PHP at jbanetwork dot com
----
rskret's code below doesn't make any sense - if you move the return line as he says you
will only ever get one instance back from the singleton() method, and subsequent calls will return
nothing. Almost definitely not what you want.
Consider the following code
<?php
$connection = DB::singleton();
$connection->exec('DROP TABLE foo');
drop_bar();
$connection->exec('DROP TABLE baz');
function bar() {
$connection = DB::singleton();
$connection->exec('DROP TABLE bar');
}
?>
You don't want 2 connections to the database, that would be slow and wasteful. So, you're
DB classes uses a singleton() method to return the same connection every time. If it returned
nothing (like rskret's code does) it would be useless to you.