<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Design Patterns &#8211; Singleton [Series][How-To]</title>
	<atom:link href="http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/</link>
	<description>Boldly goes where no coder has gone before... and other ramblings :)</description>
	<lastBuildDate>Sun, 22 Aug 2010 21:57:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Nikolaos Dimopoulos</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-106</link>
		<dc:creator>Nikolaos Dimopoulos</dc:creator>
		<pubDate>Sat, 23 Jan 2010 20:00:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-106</guid>
		<description>Yes that would work. Another way of doing this is to use an init() function which would accept all these parameters, connect to the database server and select the database. It is all up to the developer on how the class should behave.

For instance on the above I would do:

Db::init($host,$user,$password,$database);
Db::getInstance()-&gt;connect();
Db::getInstance()-&gt;selectDb();
$sql = &#039;SELECT * FROM users&#039;;
Db::getInstance()-&gt;query($sql);

This splits the implementation in different methods and you can do the lazy connect with a lot of flexibility. Instead of connecting to the server and selecting the database you can now reuse the connection to the same database server but different database. All you have to do is call 

Db::getInstance()-&gt;selectDb($database);

The additional code that I am thinking is below:


    private static $_host     = &#039;&#039;;
private static $_user     = &#039;&#039;;
private static $_pass     = &#039;&#039;;
private static $_database = &#039;&#039;;

public static function init($host, $user, $password, $database)
{
    $self::_host     = $host;
    $self::_user     = $user;
    $self::_pass     = $password;
    $self::_database = $database;
}
	
public function connect()
{
    $this-&gt;_conn = mysql_connect($this-&gt;_host, $this-&gt;_user, $this-&gt;_pass);
    if (!$this-&gt;_conn) {
        throw new Exception(&#039;Cannot connect to the database :&#039; . mysql_error());
    }
}
	
public function selectDb($database = &#039;&#039;)
{
    if (!$this-&gt;_conn) {
        throw new Exception(&#039;No database connection. Please connect to a server first.&#039;);
    }

    $this-&gt;_database = ($database) ? $database : $this-&gt;_database;

    $db = mysql_select_db($this-&gt;_database, $this-&gt;_conn);
    if (!$db) {
        throw new Exception(&#039;Cannot select the database : &#039; . mysql_error());
    }
}</description>
		<content:encoded><![CDATA[<p>Yes that would work. Another way of doing this is to use an init() function which would accept all these parameters, connect to the database server and select the database. It is all up to the developer on how the class should behave.</p>
<p>For instance on the above I would do:</p>
<p>Db::init($host,$user,$password,$database);<br />
Db::getInstance()->connect();<br />
Db::getInstance()->selectDb();<br />
$sql = &#8216;SELECT * FROM users&#8217;;<br />
Db::getInstance()->query($sql);</p>
<p>This splits the implementation in different methods and you can do the lazy connect with a lot of flexibility. Instead of connecting to the server and selecting the database you can now reuse the connection to the same database server but different database. All you have to do is call </p>
<p>Db::getInstance()->selectDb($database);</p>
<p>The additional code that I am thinking is below:</p>
<p>    private static $_host     = &#8221;;<br />
private static $_user     = &#8221;;<br />
private static $_pass     = &#8221;;<br />
private static $_database = &#8221;;</p>
<p>public static function init($host, $user, $password, $database)<br />
{<br />
    $self::_host     = $host;<br />
    $self::_user     = $user;<br />
    $self::_pass     = $password;<br />
    $self::_database = $database;<br />
}</p>
<p>public function connect()<br />
{<br />
    $this->_conn = mysql_connect($this->_host, $this->_user, $this->_pass);<br />
    if (!$this->_conn) {<br />
        throw new Exception(&#8216;Cannot connect to the database :&#8217; . mysql_error());<br />
    }<br />
}</p>
<p>public function selectDb($database = &#8221;)<br />
{<br />
    if (!$this->_conn) {<br />
        throw new Exception(&#8216;No database connection. Please connect to a server first.&#8217;);<br />
    }</p>
<p>    $this->_database = ($database) ? $database : $this->_database;</p>
<p>    $db = mysql_select_db($this->_database, $this->_conn);<br />
    if (!$db) {<br />
        throw new Exception(&#8216;Cannot select the database : &#8216; . mysql_error());<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jani Hartikainen</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-104</link>
		<dc:creator>Jani Hartikainen</dc:creator>
		<pubDate>Sat, 23 Jan 2010 19:38:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-104</guid>
		<description>Yeah looks like the code I just wrote in the comment won&#039;t actually work... public static *function*, self:: instead of $this... Haven&#039;t written PHP code in a while :D</description>
		<content:encoded><![CDATA[<p>Yeah looks like the code I just wrote in the comment won&#8217;t actually work&#8230; public static *function*, self:: instead of $this&#8230; Haven&#8217;t written PHP code in a while <img src='http://www.niden.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jani Hartikainen</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-102</link>
		<dc:creator>Jani Hartikainen</dc:creator>
		<pubDate>Sat, 23 Jan 2010 19:37:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-102</guid>
		<description>Here&#039;s an idea: Instead of using an include, how about using &quot;lazy connect&quot;?

Kinda like this.. (lets hope this shows up ok)

Db::connect(&#039;user&#039;, &#039;pass&#039;, &#039;db&#039;, &#039;host&#039;);

class Db {
  public static connect($user, $pass, $db, $host) {
    $this-&gt;_user = $user;
    //etc.
  }

  public static getInstance() {
    //if no connection, use vars set in connect to open new
  }
}

You would then initialize the database connection using the connect method somewhere in your code. Assuming you&#039;re including a file or routing all calls through index.php, it could be run there and the rest of the code does not need to care about it.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s an idea: Instead of using an include, how about using &#8220;lazy connect&#8221;?</p>
<p>Kinda like this.. (lets hope this shows up ok)</p>
<p>Db::connect(&#8216;user&#8217;, &#8216;pass&#8217;, &#8216;db&#8217;, &#8216;host&#8217;);</p>
<p>class Db {<br />
  public static connect($user, $pass, $db, $host) {<br />
    $this-&gt;_user = $user;<br />
    //etc.<br />
  }</p>
<p>  public static getInstance() {<br />
    //if no connection, use vars set in connect to open new<br />
  }<br />
}</p>
<p>You would then initialize the database connection using the connect method somewhere in your code. Assuming you&#8217;re including a file or routing all calls through index.php, it could be run there and the rest of the code does not need to care about it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tweets that mention Design Patterns – Singleton [Series][How-To] -- Topsy.com</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-92</link>
		<dc:creator>Tweets that mention Design Patterns – Singleton [Series][How-To] -- Topsy.com</dc:creator>
		<pubDate>Sat, 23 Jan 2010 09:12:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-92</guid>
		<description>[...] This post was mentioned on Twitter by Nikolaos Dimopoulos and CareerSoft, Design4people (rss). Design4people (rss) said: Nikolaos Dimopoulos: Design Patterns – Singleton [Series][How-To] http://goo.gl/fb/xT2N #designpatterns #howto #ph... http://bit.ly/8M8yuA [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was mentioned on Twitter by Nikolaos Dimopoulos and CareerSoft, Design4people (rss). Design4people (rss) said: Nikolaos Dimopoulos: Design Patterns – Singleton [Series][How-To] <a href="http://goo.gl/fb/xT2N" rel="nofollow">http://goo.gl/fb/xT2N</a> #designpatterns #howto #ph&#8230; <a href="http://bit.ly/8M8yuA" rel="nofollow">http://bit.ly/8M8yuA</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-86</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</dc:creator>
		<pubDate>Sat, 23 Jan 2010 04:32:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-86</guid>
		<description>[...] is the original post: Design Patterns – Singleton [Series][How-To]   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] is the original post: Design Patterns – Singleton [Series][How-To]   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-84</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</dc:creator>
		<pubDate>Fri, 22 Jan 2010 23:40:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-84</guid>
		<description>[...] this article: Design Patterns – Singleton [Series][How-To]   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] this article: Design Patterns – Singleton [Series][How-To]   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-82</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Drakz Free Online Service</dc:creator>
		<pubDate>Fri, 22 Jan 2010 21:50:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-82</guid>
		<description>[...] more here: Design Patterns – Singleton [Series][How-To]   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] more here: Design Patterns – Singleton [Series][How-To]   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Coder Online</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-80</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Coder Online</dc:creator>
		<pubDate>Fri, 22 Jan 2010 21:44:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-80</guid>
		<description>[...] See the original post: Design Patterns – Singleton [Series][How-To] [...]</description>
		<content:encoded><![CDATA[<p>[...] See the original post: Design Patterns – Singleton [Series][How-To] [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Linux Affinity</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-78</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Linux Affinity</dc:creator>
		<pubDate>Fri, 22 Jan 2010 21:25:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-78</guid>
		<description>[...] is the original post: Design Patterns – Singleton [Series][How-To]      Posted in: How To&#039;s ADD [...]</description>
		<content:encoded><![CDATA[<p>[...] is the original post: Design Patterns – Singleton [Series][How-To]      Posted in: How To&#39;s ADD [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Design Patterns – Singleton [Series][How-To] &#124; Neorack Tutorials</title>
		<link>http://www.niden.net/2010/01/design-patterns-singleton-series-how-to/comment-page-1/#comment-76</link>
		<dc:creator>Design Patterns – Singleton [Series][How-To] &#124; Neorack Tutorials</dc:creator>
		<pubDate>Fri, 22 Jan 2010 19:54:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.niden.net/?p=205#comment-76</guid>
		<description>[...] sections was a different php script (header. php , menu. php , content      See the article here: Design Patterns – Singleton [Series][How-To]   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] sections was a different php script (header. php , menu. php , content      See the article here: Design Patterns – Singleton [Series][How-To]   Share and [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
