<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Syntactic sugar &#187; Database</title>
	<atom:link href="http://syntacticsugar.nl/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://syntacticsugar.nl</link>
	<description>Random ramblings of another web-techy</description>
	<lastBuildDate>Sat, 09 Jan 2010 21:24:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finally : a new Crudder-release!</title>
		<link>http://syntacticsugar.nl/2009/11/28/finally-a-new-crudder-release/</link>
		<comments>http://syntacticsugar.nl/2009/11/28/finally-a-new-crudder-release/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 12:02:06 +0000</pubDate>
		<dc:creator>buTTon</dc:creator>
				<category><![CDATA[CMS]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[PHP-Scaffold]]></category>
		<category><![CDATA[Scaffold]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://syntacticsugar.nl/?p=296</guid>
		<description><![CDATA[It took a while.. a long while actually.. but I finally made a new release of Crudder. This new release 0.50 has a lot of new features and bugfixes, to name a few;

New Field-type: HTML-editor
New Field-type: File-upload
New Field-type: Enum
Export to CSV
Adding records from the many-2-many-editor
Bugfixes; language-support works better, datefield has been fixed, the many-2-many-editor should [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_297" class="wp-caption alignright" style="width: 160px"><a href="http://syntacticsugar.nl/wp-content/uploads/2009/11/screenshot_001.png"><img class="size-thumbnail wp-image-297" title="screenshot_001" src="http://syntacticsugar.nl/wp-content/uploads/2009/11/screenshot_001-150x150.png" alt="Crudder in action" width="150" height="150" /></a><p class="wp-caption-text">Crudder in action</p></div>
<p>It took a while.. a long while actually.. but I finally made a new release of <a href="http://syntacticsugar.nl/crudder/">Crudder</a>. This new release 0.50 has a lot of new features and bugfixes, to name a few;</p>
<ul>
<li>New Field-type: HTML-editor</li>
<li>New Field-type: File-upload</li>
<li>New Field-type: Enum</li>
<li>Export to CSV</li>
<li>Adding records from the many-2-many-editor</li>
<li>Bugfixes; language-support works better, datefield has been fixed, the many-2-many-editor should work better</li>
</ul>
<p><a href="http://www.crudder.net/demo.php" target="_blank">Check out the DEMO</a> or <a href="http://syntacticsugar.nl/crudder/">read more</a></p>
]]></content:encoded>
			<wfw:commentRss>http://syntacticsugar.nl/2009/11/28/finally-a-new-crudder-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Putting the Singleton-pattern to good use</title>
		<link>http://syntacticsugar.nl/2009/05/03/putting-the-singleton-pattern-to-good-use/</link>
		<comments>http://syntacticsugar.nl/2009/05/03/putting-the-singleton-pattern-to-good-use/#comments</comments>
		<pubDate>Sun, 03 May 2009 06:35:16 +0000</pubDate>
		<dc:creator>buTTon</dc:creator>
				<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[Singleton]]></category>

		<guid isPermaLink="false">http://syntacticsugar.nl/?p=167</guid>
		<description><![CDATA[I like the Singleton-pattern, it saves a lot of trouble on creating objects that you need throughout your code and need to keep their state. Though it&#8217;s very tempting, do not use the singleton too much; see here why.
There are some pretty good uses for a Singleton though; a registry and the database-connection;
Download example code [...]]]></description>
			<content:encoded><![CDATA[<p>I like the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">Singleton-pattern</a>, it saves a lot of trouble on creating objects that you need throughout your code and need to keep their state. Though it&#8217;s very tempting, do not use the singleton too much; see <a href="http://www.sitepoint.com/blogs/2008/02/13/whats-so-bad-about-the-singleton/" target="_blank">here</a> why.</p>
<p>There are some pretty good uses for a Singleton though; a registry and the database-connection;</p>
<p><span id="more-167"></span><a href="http://syntacticsugar.nl/upload/singletons.zip">Download example code here</a></p>
<p><strong>Database-connection</strong></p>
<p>I use <a href="http://adodb.sourceforge.net/" target="_self">ADODB</a> to access databases, don&#8217;t ask me why, I just do. ADODB uses a DSN to connect to a database, which looks something like:</p>
<pre><code>mysql://dbUser:dbPass@localhost/someDb</code></pre>
<p>Normally you&#8217;d use <em>$connection = &amp;ADONewConnection($DSN)</em> to connect to the database and run your query (<em>$rs = $connection-&gt;execute($sqlQuery) </em>). The problem here; you keep creating new connections anywhere you run a query, you need to pass the value for $DSN into your objects,  you&#8217;ve made your class depend on ADODB and it take 2 lines of code <img src='http://syntacticsugar.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   You can bypass these &#8216;problems&#8217; by using a Singleton to connect to your database and run a query:</p>
<pre><code>$rs = basicDBConn::getConnection()-&gt;execute($sqlQuery);</code></pre>
<p>Now, what is wrong here? Did you see $DSN anywhere?<br />
Nope&#8230; This statement assumes you&#8217;ve passed $DSN already somewhere before, in fact the <em>first</em> time you call <em>getConnection()</em> from this singleton you must pass $DSN as argument in getConnection(). The first time you call getConnection() is the moment the singleton creates an instance of itself, and uses $DSN to connect to the database (<em>&amp;ADONewConnection($DSN)). </em>After this initialization the connection is kept within the Singleton, and $DSN isn&#8217;t needed anymore. Take a look at the files, basicDBConn.php in particular to get an idea of how the Singleton creates an instance of itself and the database-connection.</p>
<p><strong>Registry</strong></p>
<p>Another good use for Singletons is a Registry-object; A registry-object is used for configuration; global settings that you might need in your code (path&#8217;s, DB-configuration etc.). Since I&#8217;m infected by the XML-virus my configuration-files always are XML :</p>
<pre><code>&lt;config&gt;
    &lt;item index="DSN"&gt;mysql://dbUser:dbPass@localhost/singletondemo&lt;/item&gt;
    &lt;item index="someKey"&gt;someValue&lt;/item&gt;
&lt;/config&gt;
</code></pre>
<p>There is a drawback though; these XML-files are <span style="text-decoration: underline;">not</span> protected by Apache by default, anyone can open them from a browser (if they are somewhere in the public root of the website). There are 2 solutions for this; prefix the file with <em>.hta</em> (in which case Apache will protect them just as .htaccess files are protected) or add the following lines to your Apache-configuration :</p>
<pre><code>&lt;Files ~ "configuration_filename.xml"&gt;
   Order allow,deny
   Deny from all
&lt;/Files&gt;</code></pre>
<p>The registry-object works the same as the Database-object; it needs a reference to a XML-file <em>the first time</em> you call it, after that it&#8217;s fully initialized and doesn&#8217;t need that reference anymore.</p>
<p>The first call to a registry object (and get the value for &#8217;some_key&#8217;):</p>
<pre><code>$someValue = basicRegistry::getInstance('/path/to/configuration/file.xml')-&gt;get("some_key");</code></pre>
<p>Any call after that:</p>
<pre><code>$someValue = basicRegistry::getInstance()-&gt;get("some_key");</code></pre>
<p><strong>Why is a singleton a good thing?</strong><br />
Getting configuration-values or a $DSN-string from within objects should be simple, you do not want to pass the path and filename to your configuration file or $DSN to a database every time you create an instance of an object. It will create dependencies and spaghetti in your code. If you make sure the registry and/or database-object are created <em>before</em> your other objects start doing something you do not need to worry about file-names and/or $DSN&#8217;s. They have been safely put away in a singleton.</p>
<p>Another very nice side-effect ; you&#8217;ve decoupled database-connections and configuration-settings. In my examples I use XML to setup the values in the registry, if you want to use some other method of keeping key-value-pairs, just modify the inner workings of the Registry-singleton, the objects using the registry won&#8217;t notice and work just as well. I usually keep session-variables in the registry, if I can&#8217;t use PHP&#8217;s $_SESSION, I can fall back onto Cookies, my classes won&#8217;t notice.</p>
<p><a href="http://syntacticsugar.nl/upload/singletons.zip">Download example code here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://syntacticsugar.nl/2009/05/03/putting-the-singleton-pattern-to-good-use/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
