<?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>Webgriffe® magazine &#187; overloading</title>
	<atom:link href="http://blog.webgriffe.com/tag/overloading/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.webgriffe.com</link>
	<description>Immaginare, creare e innovare.</description>
	<lastBuildDate>Wed, 21 Jul 2010 13:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP Object Overloading</title>
		<link>http://blog.webgriffe.com/php-object-overloading/</link>
		<comments>http://blog.webgriffe.com/php-object-overloading/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 08:05:52 +0000</pubDate>
		<dc:creator>Manuele Menozzi</dc:creator>
				<category><![CDATA[Per chi fa Web]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[linguaggio a oggetti]]></category>
		<category><![CDATA[magic methods]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[overloading]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.webgriffe.com/?p=511</guid>
		<description><![CDATA[Come noto, il linguaggio PHP è una delle tecnologie lato server più utilizzate dagli sviluppatori web. I motivi di questa popolarità sono principalmente due: Fornisce un&#8217;API specifica per interagire con Apache, il web server più diffuso al mondo. Ha una sintassi molto simile al C. Uno dei linguaggi di programmazione che hanno fatto la storia [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-thumbnail wp-image-561" title="PHP" src="http://blog.webgriffe.com/wp-content/uploads/2009/06/php-150x150.jpg" alt="PHP" width="150" height="150" />Come noto, il linguaggio <strong>PHP</strong> è una delle tecnologie lato server più utilizzate dagli sviluppatori web. I motivi di questa popolarità sono principalmente due:</p>
<ul>
<li>Fornisce un&#8217;API specifica per interagire con <strong>Apache</strong>, il web server più diffuso al mondo.</li>
<li>Ha una sintassi molto simile al <strong>C</strong>. Uno dei linguaggi di programmazione che hanno fatto la storia dell&#8217;informatica.</li>
</ul>
<p>Dalla versione 5 <strong>PHP</strong> è stato arricchito di nuovi aspetti permettendo la nascita ed il consolidamento di numerosi framework. Tra le caratteristiche di questo linguaggio ce n&#8217;è una che mi affascina particolarmente ed è l&#8217;<strong>object overloading</strong>. Generalmente con il termine <em>overloading</em>, ci si riferisce a quella caratteristica di un linguaggio che permette di dichiare più metodi con la stessa firma ma con numero e/o tipi di parametri diversi. In <strong>PHP</strong>, invece, l&#8217;<strong>object overloading</strong> è la caratteristica che permette agli oggetti di una classe, <strong></strong>di interpretare chiamate a metodi o attributi che non sono stati definiti. Tutto ciò è possibile grazie ai cosiddetti &#8220;<strong>magic methods</strong>&#8221; che, una volta definiti, permetto di intercettare tutte le chiamate inconsistenti. Questi metodi sono:</p>
<ul>
<li><em>__get(string $name)</em>: viene chiamato quando si cerca di leggere il valore di un attributo che non è stato dichiarato.</li>
<li><em>__set(string $name, mixed $value)</em>: viene chiamato quando si cerca di scrivere il valore di un attributo che non è stato dichiarato.</li>
<li><em>__isset(string $name)</em>: viene chiamato quando si utilizzano i metodi <em>isset </em>o <em>empty</em> su un attributo che non è stato dichiarato.</li>
<li><em>__unset(string $name)</em>: viene chiamato quando si utilizza il metodo <em>unset</em> su un attributo che non è stato dichiarato.</li>
<li><em>__call(string $name, array $arguments)</em>: viene chiamato quando si chiama un metodo che non è stato definito.</li>
<li><em>__callStatic(string $name, array $arguments)</em>: come il metodo precedente ma per le chiamate statiche (ad esempio <em>NomeClasse::metodo()</em>).</li>
</ul>
<p>Utilizzando questi metodi è possibile creare classi i cui attributi e metodi sono dinamici. Ad esempio si consideri la seguente classe:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**  Array dei dati overloaded  */</span>
  <span style="color: #000000; font-weight: bold;">private</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>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * L'overloading non viene usato su
   * questo attributo perchè è dichiarato.
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$declared</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Per questo attributo viene usato
   * l'overloading solo se si tenta di accedere
   * dall'esterno della classe.
   */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$hidden</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</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;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</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> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</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: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#41;</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>data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$trace</span> <span style="color: #339933;">=</span> <span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'Undefined property via __get(): '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span>
      <span style="color: #0000ff;">' in '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$trace</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span>
      <span style="color: #0000ff;">' on line '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$trace</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'line'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
      <span style="color: #009900; font-weight: bold;">E_USER_NOTICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</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> __isset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</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> __unset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</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>Ora immaginiamo che il nostro script PHP instanzi un oggetto della classe <em>Test</em> di cui sopra:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * La prossima istruzione genererà una chiamata al metodo
 * __set definito in precedenza. I parametri $name e $value
 * assumeranno rispettivamente i valori 'attrOverloaded' e 'ciao mondo!'.
 * Il metodo __set quindi aggiungerà l'indice 'attrOverloaded'
 * all'array $data e ne associerà il valore 'ciao mondo!'
 */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>attrOverloaded <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ciao mondo!'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * La prossima istruzione genererà una chiamata al metodo
 * __get definitio in precedenza. Il parametro $name avrà
 * il valore 'attrOverloaded'. Il metodo restituirà il
 * rispettivo valore presente nell'array $data ovvero
 * 'ciao mondo!'
 */</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>attrOverloaded<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Per la prossima istruzione non verrà utilizzato
 * l'overloading in quando declared è un attributo
 * dichiarato dell'oggetto.
 */</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>declared<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Per la prossima istruzione verrà utilizzato
 * l'overloading in quanto hidden è un attributo
 * privato ma non essendo però definito nell'array
 * $data verrà lanciato l'errore.
 */</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>hidden<span style="color: #339933;">;</span></pre></div></div>

<p>Se è chiaro come funziona potete divertirvi a prevedere l&#8217;output dello script (e magari postarlo nei commenti).</p>
<p>Uno svantaggio dell&#8217;utilizzo di questa tecnica è che l&#8217;esecuzione di questi metodi è 4 volte più lenta di un normale metodo di un oggetto. Inoltre, si perde la nozione di attributi privati o protetti.  La comodità di questi metodi però ne favorisce il suo utilizzo. Per citare un esempio pratico, si consideri che uno degli impieghi più frequenti di <strong>PHP,</strong> è quello di utilizzarlo per creare <em>model</em>. I <em>model</em> sono classi che modellano i dati di una qualsiasi entità che solitamente risiede su database. Ogni volta che si vuole modificare un&#8217;entità già esistente occorre anche aggiornare il <em>model</em> corrispondente.  Ancora, se si aggiunge una nuova entità su database ecco che occorre creare un apposito <em>model</em>. Utilizzando l&#8217;<strong>object overloading</strong>, invece, è possibile realizzare un <em>model</em> universale in grado di adattarsi a qualsiasi entità con qualsiasi attributo. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.webgriffe.com/php-object-overloading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
