<?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>Ciarpame {.com} &#187; PHP</title>
	<atom:link href="http://www.ciarpame.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ciarpame.com</link>
	<description>Useful stuffs for the masses. Useless things for few.</description>
	<lastBuildDate>Tue, 01 Dec 2009 13:03:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha</generator>
		<item>
		<title>La funzione __autoload di PHP</title>
		<link>http://www.ciarpame.com/2009/12/01/la-funzione-__autoload-di-php/</link>
		<comments>http://www.ciarpame.com/2009/12/01/la-funzione-__autoload-di-php/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 12:54:41 +0000</pubDate>
		<dc:creator>Mattia Palugan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=667</guid>
		<description><![CDATA[La funzione __autoload di PHP è una funzione molto utile per includere delle classi solo quando servono e senza doverle includere a mano. Se esiste, questa funzione, infatti, viene richiamata quando si tenta di utilizzare una classe o un&#8217;interfaccia che non è stata ancora dichiarata. Ha un unico parametro che è il nome della classe [...]]]></description>
			<content:encoded><![CDATA[<p>La funzione <a title="PHP: Autoloading Classes" href="http://www.php.net/autoload">__autoload</a> di PHP è una funzione molto utile per includere delle classi solo quando servono e senza doverle includere a mano.<br />
<span id="more-667"></span><br />
Se esiste, questa funzione, infatti, viene richiamata quando si tenta di utilizzare una classe o un&#8217;interfaccia che non è stata ancora dichiarata.<br />
Ha un unico parametro che è il nome della classe che è possibile manipolare per arrivare al nome del file che la include e non restituisce nulla.<br />
Ecco un esempio dove le classi sono contenute in una cartella con il percorso definito nella costante <em>_CLASS</em> e in file con il nome “nomeclasse.class.php”:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
* Autoload delle classi
* @param string $c nome della classe
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span>  _CLASS<span style="color: #339933;">.</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.class.php'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/12/01/la-funzione-__autoload-di-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>La funzione serialize di PHP</title>
		<link>http://www.ciarpame.com/2009/06/02/la-funzione-serialize-di-php/</link>
		<comments>http://www.ciarpame.com/2009/06/02/la-funzione-serialize-di-php/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 13:31:40 +0000</pubDate>
		<dc:creator>Mattia Palugan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unserialize]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=630</guid>
		<description><![CDATA[Delle volte programmando in PHP si ha bisogno di salvare in un database o in una costante una variabile complessa come un array o un oggetto per poterla utilizzare in seguito. Per farlo si può utilizzare la funzione serialize. La funzione serialize accetta un parametro, che è la variabile da serializzare, e restituisce una stringa. [...]]]></description>
			<content:encoded><![CDATA[<p>Delle volte programmando in PHP si ha bisogno di salvare in un database o in una costante una variabile complessa come un array o un oggetto per poterla utilizzare in seguito. Per farlo si può utilizzare la funzione <a title="PHP: serialize" href="http://it.php.net/serialize">serialize</a>.<br />
<span id="more-630"></span><br />
La funzione <strong>serialize</strong> accetta un parametro, che è la variabile da serializzare, e restituisce una stringa. Il parametro può essere dei seguenti tipi: booleano, intero, numero con virgola mobile, stringhe, array o oggetto.<br />
La stringa restituita può essere riportata allo stato precedente utilizzando la funzione <a title="PHP: unserialize" href="http://it.php.net/unserialize">unserialize</a>. In caso di errore restituisce falso e un errore di tipo <em>Notice</em>.<br />
Inoltre la funzione <strong>serialize</strong>, in caso il parametro da serializzare sia un oggetto, cercherà di invocare, se presente, il metodo magico <em>__sleep()</em>. Allo stesso modo la funzione unserialize cercherà di invocare il metodo magico <em>__wakeup()</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/06/02/la-funzione-serialize-di-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizzare Gravatar nel proprio sito</title>
		<link>http://www.ciarpame.com/2009/01/23/utilizzare-gravatar-nel-proprio-sito/</link>
		<comments>http://www.ciarpame.com/2009/01/23/utilizzare-gravatar-nel-proprio-sito/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 08:08:25 +0000</pubDate>
		<dc:creator>Mattia Palugan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[gravatar]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=613</guid>
		<description><![CDATA[Gravatar è l&#8217;abbreviazione di globally recognized avatar. È un servizio creato da Tom Werner che permette di utilizzare lo stesso avatar su più siti. Ogni utente ha la possibilità di inserire più indirizzi e-mail a cui associare degli avatar da poter utilizzare sui siti che utilizzano Gravatar. Per molti CMS (Joomla, WordPress, ecc.) esistono già [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Gravatar - Globally Recognized Avatars" href="http://www.gravatar.com/" target="_blank">Gravatar</a> è l&#8217;abbreviazione di <em>globally recognized avatar</em>. È un servizio creato da Tom Werner che permette di utilizzare lo stesso avatar su più siti. Ogni utente ha la possibilità di inserire più indirizzi e-mail a cui associare degli avatar da poter utilizzare sui siti che utilizzano Gravatar.<br />
Per molti CMS (Joomla, WordPress, ecc.) esistono già dei plug-in specifici per utilizzare Gravatar, ma se non si utilizzano questi CMS non è molto difficile implementare uno script adatto allo scopo.<br />
<span id="more-613"></span><br />
Bisogna inserire un&#8217;immagine dinamica richiamandola dai server di Gravatar.com. La prima parte dell&#8217;indirizzo è “http://www.gravatar.com/avatar/” e la seconda è l&#8217;hash <a title="MD5 - Wikipedia" href="http://it.wikipedia.org/wiki/MD5" target="_blank">MD5</a> dell&#8217;indirizzo e-mail dell&#8217;utente seguito dell&#8217;estensione “.jpg”.<br />
A questo punto possiamo aggiungere tutti i parametri facoltativi come query string (metodo get).<br />
Con il parametro “size” o “s” possiamo decidere la dimensione in pixel dell&#8217;avatar che può andare da un minimo di 1 a un massimo di 512. Il valore di default è 80 pixel.<br />
Con il parametro “rating” o “r” possiamo indicare il tipo di avatar (da quello adatto per tutti a quello che include contenuti espliciti) che può essere “g”, “pg”, “r” o “x”. Il valore di default è “g”.<br />
Con il parametro “default” o “d” possiamo scegliere quale immagine visualizzare se l&#8217;e-mail non è presente nel database di Gravatar. Si può inserire il percorso di un&#8217;immagine oppure si possono inserire i valori <a title="WP_Identicon" href="http://scott.sherrillmix.com/blog/blogger/wp_identicon/" target="_blank">“identicon”</a>, <a title="WP_MonsterID" href="http://scott.sherrillmix.com/blog/blogger/wp_monsterid/" target="_blank">“monsterid”</a> o <a title="Wavatars" href="http://www.shamusyoung.com/twentysidedtale/?p=1462" target="_blank">“wavatar”</a>. Se non viene specificato, si visualizzerà il simbolo di Gravatar (una G) in grigio.<br />
Ecco quindi un esempio di Gravatar con il mio indirizzo e-mail, 100 pixel di lato, adatto a tutti i tipi di visitatori e, se non è presente, verrà visualizzato il logo della <a title="SiS s.r.l. - Soluzioni Informatiche e di Sistema" href="http://www.soluzionisis.com/" target="_blank">SiS s.r.l.</a>:</p>
<div class="wp-caption aligncenter" style="width: 110px"><a href="http://www.gravatar.com/avatar/36ac00adbe6247f505c702d904f7f44a.jpg?s=100&amp;r=g&amp;d=http%3A%2F%2Fwww.soluzionisis.com%2Finc%2Fimg%2Flogo.jpg"><img title="Esempio di Gravatar" src="http://www.gravatar.com/avatar/36ac00adbe6247f505c702d904f7f44a.jpg?s=100&amp;r=g&amp;d=http%3A%2F%2Fwww.soluzionisis.com%2Finc%2Fimg%2Flogo.jpg" alt="Esempio di Gravatar" width="100" height="100" /></a><p class="wp-caption-text">Esempio di Gravatar</p></div>
<p><a href="http://www.gravatar.com/avatar/36ac00adbe6247f505c702d904f7f44a.jpg?s=100&amp;r=g&amp;d=http%3A%2F%2Fwww.soluzionisis.com%2Finc%2Fimg%2Flogo.jpg" target="_blank">http://www.gravatar.com/avatar/36ac00adbe6247f505c702d904f7f44a.jpg?s=100&amp;r=g&amp;d=http%3A%2F%2Fwww.soluzionisis.com%2Finc%2Fimg%2Flogo.jpg</a></p>
<p>Per avere lo stesso risultato in PHP si può utilizzare questo script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'indirizzo@email.it'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rating</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'g'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$default</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.soluzionisis.com/inc/img/logo.jpg'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.gravatar.com/avatar/'</span><span style="color: #339933;">.</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.jpg?s='</span><span style="color: #339933;">.</span><span style="color: #000088;">$size</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;r='</span><span style="color: #339933;">.</span><span style="color: #000088;">$rating</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;amp;d='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$default</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; alt=&quot;Avatar&quot; width=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$size</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; height=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$size</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/23/utilizzare-gravatar-nel-proprio-sito/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Far Future Expires Header: come e perchè (IIS, Apache, Tomcat, PHP, ASP, JSP)</title>
		<link>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/</link>
		<comments>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 11:42:12 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Web Servers]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=415</guid>
		<description><![CDATA[Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore. Sicuramente una condizione ottimale per ottenere prestazioni migliori della [...]]]></description>
			<content:encoded><![CDATA[<p>Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore.</p>
<p>Sicuramente una condizione ottimale per ottenere prestazioni migliori della propria web app o del proprio sito web è impostare il cosidetto <span style="text-decoration: underline;">Far Future Expire Header</span> degli elementi prensenti nelle pagine e delle pagine stesse.</p>
<p>In questo post cercherò di descrivere alcune tecniche per impostare tale condizione.</p>
<p><span id="more-415"></span></p>
<p>Cito dal sito di <a title="YSlow for Firebug" href="http://developer.yahoo.com/yslow/" target="_blank">YSlow</a>:</p>
<blockquote><p>Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on <em>all</em> components including scripts, stylesheets, and Flash components.</p></blockquote>
<p>Quindi in pratica che fa?:-)</p>
<p>L&#8217;Expire Header definisce la scadenza dell&#8217;oggetto che il browser preleva dal server e deposita nella cache del client. Impostando la &#8220;data di scadenza&#8221; ad un periodo molto lontano (Far Future per l&#8217;appunto) diciamo al browser di non richiamare più tale componente durante le succesive visite alla pagina che lo conteneva, rendendo di fatto la navigazione più veloce. Inoltre, applicando questa tecnica ad elementi comuni a tutte le pagine (es: background o file javascript o file css) l&#8217;utente usufruirà dei benefici di questa tecnica fin dalla seconda pagina che andrà a visualizzare sul nostro sito.</p>
<p>Vediamo come fare.</p>
<p><strong>Tramite l&#8217;uso di .htaccess (Apache)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;FilesMatch &quot;\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|php|html|htm|php4|php5)$&quot;&gt;
Header set Expires &quot;Thu, 2 Feb 2010 20:00:00 GMT&quot;
&lt;/FilesMatch&gt;</pre></div></div>

<p>con l&#8217;istruzione FileMatch indichiamo a quale tipo di file applicare le regole all&#8217;interno del blocco di codice e con &#8220;Header set Expires&#8221; andremo a dire ad Apache di <i>servire</i> il contenuto indicato con una data di scadenza molto lontana. Nell&#8217;esempio ho usato il 2 Febbraio 2010, alle 8 di sera.</p>
<p><strong>Tramite le impostazioni di IIS 6.0</strong></p>
<ol>
<li>Aprite il pannello di controllo di Internet Information Services (IIS).</li>
<li>Espandete &#8220;LocalMachine&#8221; sul pannelo di sinistra e succesivamente cliccate su Web Sites</li>
<li>Click con il tasto destro sul sito interessato e selezionate Properties.</li>
<li>Nel tab HTTP Headers, attivate la checkbox &#8220;Enable Content Expiration&#8221; e succesivamente selezionate l&#8217;opzione &#8220;Expire On&#8221;. e impostare il 2 Febbraio 2010 alle 8 di sera</li>
<li>Riavviate tutti i servizi IIS.</li>
</ol>
<p><strong>Con PHP</strong><br />
File far_future_loader.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/javascript; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>con il codice sopra potete, ad esempio, raggruppare tutti i vostri file javascript in un unico grande file .php, modificare l&#8217;header Content-type in modo che venga interpretato correttamente dal browser come text/javascript e poi modificarne a piaciemento la deata di scandenza.<br />
Successivamente nell&#8217;header delle vostre pagine, anzichè includere i singoli file javascript con l&#8217;istruzione:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/scripts.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>userete</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/far_future_loader.php&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Potete usare il file far_future_loader.php per ogni tipo di file, semplicemente cambiano il Content-type dei file che andrete ad includere, ad esempio, se volete averne uno anche per i file .css dovrete scrivere:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/css; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/style.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>e successivamente nell&#8217;header delle vostre pagine, esattamente come avete fatto per il file javascript, sostituirete l&#8217;istruzione:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; type=&quot;text/css&quot; /&gt;</pre></div></div>

<p>con</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/far_future_loader.php&quot; type=&quot;text/css&quot; /&gt;</pre></div></div>

<p><strong>Con ASP</strong><br />
la procedura è identica a quella per PHP con la sola differenza che in ASP gli header si setttano con i seguenti comandi:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">CacheControl</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;must-revalidate&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Expires</span> <span style="color: #006600; font-weight: bold;">=</span> -<span style="color: #800000;">1</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p><strong>Con JSP (Tomcat)</strong><br />
Grazie a <a href="http://www.jguru.com/guru/viewfaqs.jsp?EID=1168738" target="_blank">Byron Tymvios</a> su <a target="_blank" href="http://www.jguru.com/faq/view.jsp?EID=1311010">jGuru</a><br />
E&#8217; possibile ottenere l&#8217;identico risultato avuto con PHP o ASP tramite i filtri. Ad esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">package com.xyz
&nbsp;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
&nbsp;
public class CacheFilter implements javax.servlet.Filter {
  FilterConfig filterConfig = null;
&nbsp;
  public void init(FilterConfig filterConfig){
    this.filterConfig = filterConfig;
  }
&nbsp;
  public void doFilter(ServletRequest req,
     ServletResponse res,
     FilterChain chain)
     throws IOException, ServletException {
    String sCache = filterConfig.getInitParameter(&quot;cache&quot;);
&nbsp;
    if(sCache != null){       ((HttpServletResponse)res).setHeader(&quot;Cache-Control&quot;, sCache);
&nbsp;
    }
&nbsp;
    chain.doFilter(req, res);
  }
&nbsp;
  public void destroy(){
    this.filterConfig = null;
  }
}</pre></div></div>

<p>e poi impostare nel file web.xml il filtro per ogni contenuto desiderato. Ecco l&#8217;esempio per i file .jpg:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">&lt;filter&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;filter-class&gt;com.xyz.CacheFilter&lt;/filter-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;cache&lt;/param-name&gt;
    &lt;param-value&gt;public, max-age=2592000&lt;/param-value&gt;
  &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;url-pattern&gt;*.jpg&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre></div></div>

<p>è tutto <img src='http://www.ciarpame.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
nella prossimo post parleremo degli ETag.<br />
Giovanni</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: da Rails a PHP, programmare agilmente (2)</title>
		<link>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/</link>
		<comments>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 11:04:52 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=459</guid>
		<description><![CDATA[Rieccoci di nuovo a parlare di active record. Nella scorsa puntata avevamo visto una implementazione base del pattern. Essa però è orfana di qualsiasi metodo per la ricerca dei record. Vediamo ora come è possibile farlo. Rails mette a disposizione diversi metodi per recuperare record dalle tabelle di un database. Il primo è quello di [...]]]></description>
			<content:encoded><![CDATA[<p>Rieccoci di nuovo a parlare di active record. Nella scorsa puntata avevamo visto una implementazione base del pattern. Essa però è orfana di qualsiasi metodo per la ricerca dei record. Vediamo ora come è possibile farlo.<br />
<span id="more-459"></span><br />
Rails mette a disposizione diversi metodi per recuperare record dalle tabelle di un database. Il primo è quello di utilizzare il costruttore della classe passando come parametro l&#8217;id del record che vogliamo leggere. In molti linguaggi di programmazione Object-Oriented è possibile effettuare l&#8217;overload del costruttore, ovvero definire più costruttori per una stessa classe, dove in ogni costruttore cambiano i parametri per numero e/o tipo. In PHP non esiste tale meccanismo (e neanche in Ruby da quanto mi risulta!). Abbiamo visto nella puntata precedente come il costruttore della classe php ActiveRecordBase accetti come unico parametro un array contenente i campi che vogliamo dare ad un nuovo record. Simuliamo ora l&#8217;overloading del costruttore in modo che accetti sia l&#8217;array che un numero che identifichi l&#8217;ID del record.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> ActiveRecordBase <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># qui ci va la dichiarazione degli attributi della classe che tralasciamo
</span>  <span style="color: #666666; font-style: italic;"># .....
</span>  <span style="color: #666666; font-style: italic;">#
</span>  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
                  <span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;"># Qui prosegue il resto del codice della classe
</span><span style="color: #666666; font-style: italic;"># ....
</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Con questa modifica la nostra classe continua a funzionare accettando 0 o 1 parametro di tipo numerico o array/hash.<br />
Vediamo ora l&#8217;implementazione di un metodo find() che permetta di passare come parametro l&#8217;ID del record che vogliamo ottenere. Per semplicità d&#8217;ora in avanti verranno omesse le dichiarazioni di classe quando non strettamente necessarie.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span>
                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
             <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Come potete vedere, l&#8217;implementazione di find() è molto simile a quella del costruttore. Ricordiamoci il principio DRY (= Don&#8217;t Repeat Yourself). Modifichiamo il costruttore e aggiungiamo un metodo privato che assegni a partire da un array i valori corrispondenti agli attributi della classe.<br />
__assignData sarà un metodo privato che assegna i valori agli attributi:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
             <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Possiamo semplificare il metodo find():</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span>
                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>E ora rivediamo il costruttore:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: da Rails a PHP, programmare agilmente (1)</title>
		<link>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/</link>
		<comments>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 12:42:19 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=274</guid>
		<description><![CDATA[Una delle feature del framework Ruby On Rails più potenti è l&#8217;implementazione del pattern ActiveRecord. Esso permette di &#8220;vedere&#8221; ogni record di una tabella del database come istanze di una classe permettendo di eseguire le normali operazioni di CRUD (Create, Read, Update, Delete) con poche righe di codice. L&#8217;implementazione di ActiveRecord in Rails è molto [...]]]></description>
			<content:encoded><![CDATA[<p>Una delle feature del framework Ruby On Rails più potenti è l&#8217;implementazione del pattern ActiveRecord. Esso permette di &#8220;vedere&#8221; ogni record di una tabella del database come istanze di una classe permettendo di eseguire le normali operazioni di CRUD (Create, Read, Update, Delete) con poche righe di codice.</p>
<p>L&#8217;implementazione di ActiveRecord in Rails è molto complessa, permette di effettuare validazioni e avere riferimenti a chiavi esterne. Vediamo un&#8217;implementazione più semplice in PHP, ma comunque efficace.<br />
Cercheremo di arrivare ad un&#8217;implementazione molto potente in qualche puntata.<br />
<span id="more-274"></span><br />
Il concetto che sta alla base di active record è di trasformare l&#8217;accesso al database in accesso a una classe.<br />
Cominciamo a definire una classe base che abbia l&#8217;accesso al database. Per semplificare utilizzeremo la classe PEAR::MDB2.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'MDB2.php'</span><span style="color: #339933;">;</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> ActiveRecordBase <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__db</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Dichiariamo un oggetto &quot;protected&quot; per l'accesso al DB</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__primary_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Definiamo una chiave primaria per la tabella. Può essere poi</span>
                                           <span style="color: #666666; font-style: italic;">// modificata per ogni singola classe che estende questa.</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__fields</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// In questo attributo di tipo array andremo a salvare i campi della nostra tabella (escluso il campo primary key)</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__table</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// In questo attributo salviamo il nome della tabella</span>
  <span style="color: #009933; font-style: italic;">/**
   * Il costruttore istanzia una connessione con il db e se sono presenti dei dati
   * li salva nell'oggetto (non nel database!)
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Definiamo un metodo per create un nuovo Record.
   * Esso è privato perché verrà utilizzato dal metodo save()
   */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// Creaimo l'istruzione SQL a partire dai campi disponibili</span>
     <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; (&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__fields <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$first</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$field</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;) VALUES (&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;?&quot;</span> <span style="color: #b1b100;">else</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,?&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$sql</span><span style="color: #339933;">:=</span><span style="color: #0000ff;">&quot;);&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$prep</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$prep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertID</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SET &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__fields <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$first</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$field</span> = ?&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$field</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$prep</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$prep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
             <span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In questa implementazione, ancora in stato &#8220;embrionale&#8221;, è possibile creare un nuovo record.<br />
Un uso molto semplice potrebbe essere creare una classe che estenda la base e istanziarne un oggetto:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ActiveRecordBase.class.php'</span>
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecordBase <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$dog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Pluto'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'7'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il precedente script inserisce un nuovo record in una tabella &#8216;dog&#8217; (stessa nome della classe che estende ActiveRecordBase) assegnando gli attributi &#8216;name&#8217; e &#8216;age&#8217;.<br />
La primary key viene implicitamente impostata come &#8220;id&#8221; (ovviamente se il campo ha l&#8217;attributo auto_increment) e il valore inserito automaticamente.<br />
Il tutto in 3 righe di codice, molto molto semplici&#8230;non è una bella comodità?<br />
Più avanti vedremo come effettuare ricerche, e quindi come implementare il metodo &#8220;find&#8221; e migliorare ulteriormente la classe base in modo da gestire meglio errori, validazioni e chiavi esterne.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ordinare un array multidimensionale e più array in PHP</title>
		<link>http://www.ciarpame.com/2008/10/24/ordinare-un-array-multidimensionale-e-piu-array-in-php/</link>
		<comments>http://www.ciarpame.com/2008/10/24/ordinare-un-array-multidimensionale-e-piu-array-in-php/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 07:00:25 +0000</pubDate>
		<dc:creator>Mattia Palugan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=257</guid>
		<description><![CDATA[Tutte le funzioni che abbiamo visto nel post precedente servono ad ordinare un array monodimensionale. Per ordinare un array multidimensionale o più array contemporaneamente si può utilizzare la funzione array_multisort. Questa funzione restituisce TRUE o FALSE in base al successo o meno dell&#8217;ordinamento e mantiene le chiavi se non sono numeriche. Accetta un numero flessibile [...]]]></description>
			<content:encoded><![CDATA[<p>Tutte le funzioni che abbiamo visto nel post precedente servono ad ordinare un array monodimensionale. Per ordinare un array multidimensionale o più array contemporaneamente si può utilizzare la funzione <a href="http://it.php.net/manual/it/function.array-multisort.php" target="_blank">array_multisort</a>.<br /><span id="more-257"></span></p>
<p>Questa funzione restituisce TRUE o FALSE in base al successo o meno dell&#8217;ordinamento e mantiene le chiavi se non sono numeriche. Accetta un numero flessibile di parametri. Il primo parametro deve essere un array e quelli seguenti possono essere array o metodi di ordinamento (SORT_ASC per ordinare in ordine crescente, SORT_DESC per l&#8217;ordine decrescente e i metodi che abbiamo già visto SORT_REGULAR, SORT_NUMERIC e SORT_STRING).</p>
<p>Nel primo esempio vediamo come ordinare più array contemporaneamente:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'e'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr1</span><span style="color: #339933;">,</span><span style="color: #000088;">$arr2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il primo array quindi sarà ordinato in questo modo 2, 2, 3, 4 e 5 e il secondo in questo modo &#8216;b&#8217;, &#8216;c&#8217;, &#8216;a&#8217;, &#8216;d&#8217; e &#8216;e&#8217;. Praticamente viene effettuato l&#8217;ordinamento del primo array e quando ci sono degli elementi uguali (nell&#8217;esempio il 2) si effettua l&#8217;ordinamento in base all&#8217;elemento con lo stesso indice del secondo array.</p>
<p>Per ordinare un array multidimensionale il ragionamento è il medesimo. Ecco un esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'e'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il risultato sarà come quello dell&#8217;esempio precedente, cioè il primo sotto-array sarà uguale al primo array dell&#8217;esempio precedente e il secondo sotto-array sarà uguale al secondo dell&#8217;esempio precedente.</p>
<p>Quindi si può dedurre che la funzione array_multisort è molto simile all&#8217;ORDER BY di SQL.<br />
Ecco una query di esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> nome<span style="color: #66cc66;">,</span>cognome <span style="color: #993333; font-weight: bold;">FROM</span> utenti <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> cognome <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">,</span>nome <span style="color: #993333; font-weight: bold;">ASC</span></pre></div></div>

<p>È uguale a questo script in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$utenti</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Pippo'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cognome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Baudo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Mickey'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cognome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Mouse'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Paolino'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cognome'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'Paperino'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cognomi</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nomi</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$utenti</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$cognomi</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cognome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$nomi</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cognomi</span><span style="color: #339933;">,</span>SORT_DESC<span style="color: #339933;">,</span>SORT_STRING<span style="color: #339933;">,</span> <span style="color: #000088;">$nomi</span><span style="color: #339933;">,</span>SORT_DESC<span style="color: #339933;">,</span>SORT_STRING<span style="color: #339933;">,</span> <span style="color: #000088;">$utenti</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>L&#8217;array <em>$utenti </em>sarà ordinato prima in modo decrescente in base ai cognomi e poi, per i cognomi uguali, in modo crescente in base ai nomi.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/24/ordinare-un-array-multidimensionale-e-piu-array-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ordinare un array monodimensionale in PHP</title>
		<link>http://www.ciarpame.com/2008/10/16/ordinare-un-array-monodimensionale-in-php/</link>
		<comments>http://www.ciarpame.com/2008/10/16/ordinare-un-array-monodimensionale-in-php/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 21:38:02 +0000</pubDate>
		<dc:creator>Mattia Palugan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=219</guid>
		<description><![CDATA[Per ordinare gli array monodimensionali PHP ci viene incontro fornendoci moltissime funzioni. La più basilare è sort che ci permette di ordinare un array dal valore più piccolo a quello più grande riassegnando le chiavi. Questa funzione restituisce TRUE o FALSE in base al successo o meno dell&#8217;ordinamento e accetta due parametri. Il primo parametro [...]]]></description>
			<content:encoded><![CDATA[<p>Per ordinare gli array monodimensionali PHP ci viene incontro fornendoci moltissime funzioni.<br />
La più basilare è <a href="http://it.php.net/manual/it/function.sort.php" target="_blank">sort</a> che ci permette di ordinare un array dal valore più piccolo a quello più grande riassegnando le chiavi. Questa funzione restituisce TRUE o FALSE in base al successo o meno dell&#8217;ordinamento e accetta due parametri. Il primo parametro è l&#8217;array che vogliamo ordinare e il secondo, facoltativo, è il metodo di ordinamento che può avere tre valori: SORT_REGULAR per comparare gli elementi in modo normale, SORT_NUMERIC per compararli in modo numerico e SORT_STRING per compararli come stringhe.</p>
<p><span id="more-219"></span><br />
Ecco un esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pippo'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pluto'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'paperino'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Dopo l&#8217;utilizzo di questa funzione potrebbe essere necessario azzerare il contatore utilizzando la funzione <a href="http://it.php.net/manual/it/function.reset.php" target="_blank">reset</a>.<br />
Le seguenti funzioni si utilizzano alla stesso modo di sort ma servono per ordinare gli array in modo diverso:</p>
<ul>
<li><a href="http://it.php.net/manual/it/function.rsort.php" target="_blank">rsort</a> ordina un array in ordine decrescente;</li>
<li><a href="http://it.php.net/manual/it/function.asort.php" target="_blank">asort</a> ordina un array in ordine crescente ma mantiene le chiavi;</li>
<li><a href="http://it.php.net/manual/it/function.arsort.php" target="_blank">arsort</a> ordina un array in ordine decrescente ma mantiene le chiavi;</li>
<li><a href="http://it.php.net/manual/it/function.ksort.php" target="_blank">ksort</a> ordina un array in base alle chiavi e non ai valori;</li>
<li><a href="http://it.php.net/manual/it/function.krsort.php" target="_blank">krsort</a> ordina un array in base alle chiavi in ordine decrescente.</li>
</ul>
<p>Per ordinare un array in modo naturale si possono usare le funzioni <a href="http://it.php.net/manual/it/function.natsort.php" target="_blank">natsort</a> e <a href="http://it.php.net/manual/it/function.natcasesort.php" target="_blank">natcasesort</a>. Entrambe ordinano in ordine crescente e mantengono le chiavi con l&#8217;unica differenza che natcasesort non fa differenza tra lettere maiuscole e minuscole. Queste funzioni non restituiscono nulla e hanno solo un parametro, l&#8217;array che si vuole ordinare.<br />
Ecco un esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pippo12.php'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pippo2.php'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pippo1.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arr</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">natsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il risultato di sort sarà un array con gli elementi in questo ordine: “pippo1.php”, “pippo12.php” e “pippo2.php”; invece il risultato di natsort sarà questo: “pippo1.php”, “pippo2.php” e “pippo12.php”.</p>
<p>È possibile poi ordinare un array in base a una funzione definita dal programmatore. Per far ciò si può utilizzare la funzione <a href="http://it.php.net/manual/it/function.usort.php" target="_blank">usort</a> che restituisce TRUE o FALSE in base al successo o meno dell&#8217;ordinamento e accetta due parametri. Il primo parametro è l&#8217;array da ordinare e il secondo è il nome della funzione definita dal programmatore.<br />
La funzione definita dal programmatore deve accettare due parametri che saranno i due valori da confrontare e deve restituire un numero intero minore di zero se il primo parametro è minore del secondo, zero se i due parametri sono uguale e un numero intero maggiore di zero se il secondo parametro è maggiore del secondo.<br />
Ecco un esempio che simula la funzione sort:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortAccordingMe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">==</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pippo'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pluto'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'paperino'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'sortAccordingMe'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Altre due funzioni simili nel funzionamento a usort sono <a href="http://it.php.net/manual/it/function.uasort.php" target="_blank">uasort</a> che mantiene le chiavi e <a href="http://it.php.net/manual/it/function.uksort.php" target="_blank">uksort</a> che ordina in base alle chiavi e non ai valori.</p>
<p>La prossima volta spiegherò come ordinare un array multidimensionale e più array contemporaneamente.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/16/ordinare-un-array-monodimensionale-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Interfacce e classi astratte&#8230;queste sconosciute</title>
		<link>http://www.ciarpame.com/2008/10/16/le-interfacce-e-classi-astrattequeste-sconosciute/</link>
		<comments>http://www.ciarpame.com/2008/10/16/le-interfacce-e-classi-astrattequeste-sconosciute/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 12:22:34 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=197</guid>
		<description><![CDATA[Ieri qualcuno mi ha chiesto se programmando in PHP avessi mai usato la keyword &#8216;implements&#8217;. Ho risposto &#8220;raramente, uso più spesso &#8216;abstract&#8217; &#8220;. Per quanto possano essere simili interfacce e classi astratte sono però due cose ben distinte. Facciamo un po&#8217; di chiarezza. Una classe astratta è una classe che non può essere istanziata, ma [...]]]></description>
			<content:encoded><![CDATA[<p>Ieri qualcuno  mi ha chiesto se programmando in PHP avessi mai usato la keyword &#8216;implements&#8217;. Ho risposto &#8220;raramente, uso più spesso &#8216;abstract&#8217; &#8220;. Per quanto possano essere simili interfacce e classi astratte sono però due cose ben distinte. Facciamo un po&#8217; di chiarezza.<br />
<span id="more-197"></span></p>
<p>Una classe astratta è una classe che non può essere istanziata, ma che può essere estesa da altre classi. Prendiamo per esempio questa classe astratta:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> Personaggio <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nome</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nome <span style="color: #339933;">=</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Ciao! Io sono &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nome<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Se tentassimo di istanziare questa classe con</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pippo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Personaggio<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pippo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>l&#8217;interprete PHP ci risponderebbe con: &#8220;Fatal error: Cannot instantiate abstract class Personaggio&#8221;.</p>
<p>Possiamo però creare una nuova classe che estenda Personaggio:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Pippo <span style="color: #000000; font-weight: bold;">extends</span> Personaggio <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pippo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>La classe Pippo eredita i metodi e gli attributi di Personaggio e ha una redifinizione del costruttore, dove si va esplicitamente a richiamare il costruttore della classe astratta, specificando però un parametro costante per questa classe (la stringa &#8220;Pippo&#8221;). Possiamo quindi istanziare Pippo ed utilizzare i metodi della classe personaggio:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pippo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pippo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$pippo</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>L&#8217;output di questo banale programma è la stringa &#8220;Ciao! Io sono Pippo&#8221;. Le classi astratte quindi servono quando si devono implementare della classi ha hanno alcune operazioni in comune. Le classi che estendono una classe astratta possono poi avere dei metodi propri o ridefinire i metodi già definiti nella classe astratta.</p>
<p>Le interfacce invece definiscono un &#8220;prototipo&#8221; di una classe. Tutte le classi che implementano un&#8217;interfaccia devono definirne tutti i metodi. Vediamo un esempio di utilizzo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Personaggio <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Pippo implements Personaggio <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nome <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Pippo&quot;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Ciao! Io sono &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>nome<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$pippo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pippo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$pippo</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Apparentemente l&#8217;uso di una classe astratta o di una interfaccia sembra pressoché indifferente, soprattutto perché PHP non è un linguaggio fortemente tipizzato, ma il concetto che sta alla base di uno o dell&#8217;altro cambia: una classe Pippo che estende una classe astratta Personaggio è una classe di tipo &#8220;Pippo&#8221; che eredita i metodi e gli attributi della classe &#8220;Personaggio&#8221;. Una classe Pippo che implementa un&#8217;interfaccia &#8220;Personaggio&#8221; è sia una classe di tipo &#8220;Pippo&#8221; che di tipo &#8220;Personaggio&#8221;.<br />
Questa differenza si noterebbe di più se si potesse dichiarare il tipo degli argomenti di una funzione (come ad esempio avviene in Java).<br />
Ammettiamo che lo si possa fare anche in PHP e che abbiamo la seguente funzione:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> incontra<span style="color: #009900;">&#40;</span>Personaggio <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$p</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>saluta<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Se usiamo le interfacce non abbiamo nessun problema a far passare come argomento un&#8217;istanza di Pippo, mentre se Pippo è una classe che ne estende una astratta Personaggio, per poter far passare come argomento della funzione una istanza di Pippo dovremmo farne il downcast a Personaggio.</p>
<p>Riepilogando: l&#8217;uso delle classi astratte che vengono estese è utile quando si devono implementare più classi che hanno metodi e attributi in comune, ma dove il &#8220;caso base&#8221; (la classe astratta) non è ammissibile.<br />
L&#8217;uso delle interfacce invece è utile quando le classi devono essere dello stesso tipo, ma ogni volta devono implementare i metodi dell&#8217;interfaccia in modo differente. Vediamo ora un esempio più concreto:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definiamo un'interfaccia Forma con un metodo getArea()</span>
<span style="color: #000000; font-weight: bold;">interface</span> Forma <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Definiamo ora le implementazioni di Forma, per semplicità sono stati omessi i costruttori. Ammettiamo che comunque abbiano dei</span>
<span style="color: #666666; font-style: italic;">// parametri per impostare gli attributi delle varie classi</span>
<span style="color: #000000; font-weight: bold;">class</span> Quadrato implements Forma <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>lato<span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>lato<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Cerchio implements Forma <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>raggio<span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>raggio<span style="color: #339933;">*</span><span style="color:#800080;">3.14</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Triangolo implements Forma <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>base<span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>altezza<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Definiamo ora una funzione in &quot;stile Java&quot; che stampi a video l'area di una forma</span>
<span style="color: #000000; font-weight: bold;">function</span> stampaArea<span style="color: #009900;">&#40;</span>Forma <span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$f</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Proviamo ad utilizzare le implementazione dell'interfaccia Forma con questa funzione</span>
<span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Triangolo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$base</span><span style="color: #339933;">,</span><span style="color: #000088;">$altezza</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Quadrato<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lato</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stampaArea<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
stampaArea<span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/16/le-interfacce-e-classi-astrattequeste-sconosciute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE 6: header Content-disposition ignorati (ignored) !!FIXED!!</title>
		<link>http://www.ciarpame.com/2008/10/13/ie-6-header-content-disposition-ignorati-ignored-fixed/</link>
		<comments>http://www.ciarpame.com/2008/10/13/ie-6-header-content-disposition-ignorati-ignored-fixed/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 21:13:43 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=179</guid>
		<description><![CDATA[Tra tutti i browser del mondo, quello che mi da più rotture è Internet Explorer. Ho smesso anche di usarlo, ma non per sentimenti di ribellione incondizionata e pinguinosa nei confronti di Microsoft, ma perché credo che Firefox sia meglio, soprattutto se di lavoro si fa lo sviluppatore web. Nonostante tutte però gli utenti usano [...]]]></description>
			<content:encoded><![CDATA[<p>Tra tutti i browser del mondo, quello che mi da più rotture è Internet Explorer. Ho smesso anche di usarlo, ma non per sentimenti di ribellione incondizionata e pinguinosa nei confronti di Microsoft, ma perché credo che Firefox sia meglio, soprattutto se di lavoro si fa lo sviluppatore web. Nonostante tutte però gli utenti usano IE, e parecchi ancora usano IE 6.<br />
<span id="more-179"></span></p>
<p>In particolare Internet Explorer 6 mi ha dato una bella gatta da pelare con i file da scaricare.<br />
In un&#8217;applicazione web che ho scritto è possibile scaricare alcuni file, solo da parte di utenti registrati. Quindi i file stanno fuori dalla visibilità del browser e vengono serviti da un file (download.php, nel mio caso) che, dopo aver fatto i relativi controlli a login e permessi vari, cambia gli header in modo da far scaricare il file scelto. Semplificando molto è una cosa simile:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> db<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
checkPermission<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM attachments WHERE id = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mime'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-disposition: attachment; filename=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Transfer-Enconding: binary&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'full_path'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Risultato? Con Firefox nessun problema, con IE 7 nessun problema (eh meno male!!) ma con IE6&#8230; disastro!! I file venivano scaricato, ma IE6 ignorava bellamente l&#8217;header content-type e l&#8217;header content-disposition, nominando il file &#8220;download.php&#8221;.</p>
<p>Le ho provate davvero tutte per ovviare a questo problema, cambiare header rigurdanti la cache,ecc. ecc., ma l&#8217;unico effetto che sono riuscito ad ottenere è stato che il file non si chiamava più download.php, ma download.pdf piuttosto che download.zip&#8230;ma io voglio il nome corretto!!!</p>
<p>Google alla mano, ho fatto mille ricerche che non hanno portato a nulla finchè non sono arrivato a questa pagina: http://www.sitefoundry.com/broken_msie.html<br />
e qui ho appreso che per un baco, IE6 in questi casi tenta sempre due richieste andando in errore e che a differenza di Firefox utilizza il protocollo HTTP 1.1 anziché 1.0. Allora mi sono detto: &#8220;non è che è tutto un problema di protocollo?!?&#8221;.<br />
Prima di:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>ho inserito la seguente riga:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.0 200 OK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>forzando così che la mia risposta sia in HTTP/1.0&#8230;e ha funzionato!!!<br />
La mia spiegazione è questa: IE cerca (per motivi di sicurezza, dicono) di indentificare autonomamente il tipo di file, ignorando i Content-type e content-disposition vari che si mettono. Forzando il protocollo HTTP a 1.0 probabilmente IE si va a trovare in una modalità compatibile col vecchio protocollo, disabilitando le feature che normalmente utilizza col protocollo HTTP/1.1.</p>
<p>Bisogna stare attenti però all&#8217;output compression: il vostro server (Apache) potrebbe avere attivo mod_gzip, il che porterebbe a &#8220;g-zippare&#8221; tutto il suo output per risparmiare banda di rete. Purtroppo il &#8220;caro vecchio&#8221; IE6 ha un po&#8217; di problemi a trattare gli header con output gzipped.<br />
La soluzione è in questo caso aggiungere una riga</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">apache_setenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no-gzip&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>al vostro script, prima di mandare in output il file da servire. Eventualmente verificate anche che sia disattivata la compressione gzip da PHP.<br />
Per disattivarla occorre aggiungere</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;zlib.output_compression&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Off&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Io ci sono impazzito 2 giorni su sta cosa&#8230;spero che possa essere d&#8217;aiuto a molti! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/13/ie-6-header-content-disposition-ignorati-ignored-fixed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

