<?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>Tellaw.org &#187; singleton</title>
	<atom:link href="http://www.tellaw.org/tag/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tellaw.org</link>
	<description>La technologie doit devenir simple...</description>
	<lastBuildDate>Fri, 02 Apr 2010 14:48:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exemple de singleton en PHP5 avec un objet de lecture de fichier de config</title>
		<link>http://www.tellaw.org/2008/04/23/exemple-de-singleton-en-php5-avec-un-objet-de-lecture-de-fichier-de-config/</link>
		<comments>http://www.tellaw.org/2008/04/23/exemple-de-singleton-en-php5-avec-un-objet-de-lecture-de-fichier-de-config/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 07:44:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=38</guid>
		<description><![CDATA[PHP 5 transforme PHP en un véritable langage objet. Il devient donc important d&#8217;utiliser l&#8217;expérience d&#8217;autres programmeur pour assurer une qualité maximum. Vous pouvez donc utiliser les design pattern en PHP5. Voici un exemple d&#8217;utilisation d&#8217;un singleton à travers la lecture d&#8217;un fichier de configuration.

Principe du singleton :
Le singleton permet de s&#8217;assurer qu&#8217;une seule instance [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5 transforme PHP en un véritable langage objet. Il devient donc important d&#8217;utiliser l&#8217;expérience d&#8217;autres programmeur pour assurer une qualité maximum. Vous pouvez donc utiliser les design pattern en PHP5. Voici un exemple d&#8217;utilisation d&#8217;un singleton à travers la lecture d&#8217;un fichier de configuration.</p>
<p><span id="more-38"></span></p>
<p><strong>Principe du singleton :</strong></p>
<p>Le singleton permet de s&#8217;assurer qu&#8217;une seule instance d&#8217;un objet donné sera instanciée pendant toute la durée de votre application. Une seule dans l&#8217;espace comme dans le temps, c&#8217;est-à-dire :</p>
<ul>
<li>l&#8217;espace représenté par la mémoire &#8211; vous êtes certain de l&#8217;unicité de l&#8217;instance à un moment donné</li>
<li>le temps &#8211; vous vous assurer de l&#8217;unicité de l&#8217;instance à chaque appel. J&#8217;entends par la que vous êtes certain que c&#8217;est la même instance que vous référer quelque soit le temps écoulé entre deux appels.</li>
</ul>
<p>(extrait de la page http://smeric.developpez.com/java/uml/singleton/)</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Config <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066;">static</span> <span style="color: #ff0000">$_instance</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #ff0000">$config</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __constrcut <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Private constrcteur</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> GetInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span>self<span style="color: #66cc66;">::</span><span style="color: #ff0000">$_instance</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			self<span style="color: #66cc66;">::</span><span style="color: #ff0000">$_instance</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> self<span style="color: #66cc66;">::</span><span style="color: #ff0000">$_instance</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadIni <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">config</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">parse_ini_file</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;conf.ini&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadIniFromPath <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">config</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">parse_ini_file</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;conf.ini&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$section</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$key</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">config</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$section</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$key</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #ff0000">$config</span> <span style="color: #66cc66;">=</span> Config<span style="color: #66cc66;">::</span><span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$config</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #006600;">loadIni</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Ici utilisation classique de l'objet ...</span>
<span style="color: #808080; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Le principe du script est simple, création d&#8217;une variable statique dans laquelle nous insérons la référence de l&#8217;objet instancié. Ainsi l&#8217;appelle à la méthode getInstance assure l&#8217;application d&#8217;utiliser toujours la même instance de cette classe, et donc de n&#8217;avoir qu&#8217;un objet de ce type en mémoire.</p>
<p>pour plus d&#8217;info consultez l&#8217;excellent site de QWIX : <a href="http://qwix.media-box.net/" target="_blank">http://qwix.media-box.net/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/23/exemple-de-singleton-en-php5-avec-un-objet-de-lecture-de-fichier-de-config/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
