<?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>Victor Stanciu &#187; PHP</title>
	<atom:link href="http://www.victorstanciu.ro/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.victorstanciu.ro</link>
	<description>Web Developer, Geek</description>
	<lastBuildDate>Sat, 10 Jul 2010 07:39:00 +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 SoapClient port bug workaround</title>
		<link>http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/</link>
		<comments>http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 11:45:15 +0000</pubDate>
		<dc:creator>Victor Stanciu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://www.victorstanciu.ro/?p=456</guid>
		<description><![CDATA[PHP&#8217;s SoapClient has a bug (PHP 5.2.10 here, still not fixed apparently) when the SOAP service must be accessed on a different port other than 80. The WSDL file is fetched correctly, but all subsequent requests are made without any port in the Host field. This causes a SoapFault exception when trying to call any [...]


<b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/ftl-jump/' rel='bookmark' title='Permanent Link: FTL jump &#8230; now!'>FTL jump &#8230; now!</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>PHP&#8217;s SoapClient has a <a href="http://bugs.php.net/bug.php?id=30359">bug</a> (PHP 5.2.10 here, still not fixed apparently) when the SOAP service must be accessed on a different port other than 80. The WSDL file is fetched correctly, but all subsequent requests are made without any port in the Host field. This causes a SoapFault exception when trying to call any of the service&#8217;s methods.</p>
<p>So if the WSDL location is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://example.com:33080/soap/server/path?WSDL</pre></div></div>

<p>All requests after fetching the WSDL file will be made to:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://example.com/soap/server/path</pre></div></div>

<p>The simplest way i could work around this was to extend SoapClient and intercept the constructor and the __doRequest method to inject the port in the location on each request:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</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: #000000; font-weight: bold;">class</span> My_SoapClient <span style="color: #000000; font-weight: bold;">extends</span> SoapClient <span style="color: #009900;">&#123;</span>
&nbsp;
    <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;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</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: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</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>_port <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</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> __doRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #000088;">$location</span><span style="color: #339933;">,</span> <span style="color: #000088;">$action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$parts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$location</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildLocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span>__doRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #000088;">$location</span><span style="color: #339933;">,</span> <span style="color: #000088;">$action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</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> buildLocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</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;">$location</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
        <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;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'scheme'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'scheme'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'://'</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span><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;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span><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;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$location</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?'</span><span style="color: #339933;">.</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It works for me, and I would like to know if it doesn&#8217;t cover your particular case :)</p>


<p><b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/ftl-jump/' rel='bookmark' title='Permanent Link: FTL jump &#8230; now!'>FTL jump &#8230; now!</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detectarea platformelor mobile in PHP</title>
		<link>http://www.victorstanciu.ro/detectarea-platformelor-mobile-in-php/</link>
		<comments>http://www.victorstanciu.ro/detectarea-platformelor-mobile-in-php/#comments</comments>
		<pubDate>Thu, 21 May 2009 13:19:06 +0000</pubDate>
		<dc:creator>Victor Stanciu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[blackberry]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[opera mini]]></category>
		<category><![CDATA[palm]]></category>
		<category><![CDATA[windows mobile]]></category>

		<guid isPermaLink="false">http://www.victorstanciu.ro/?p=335</guid>
		<description><![CDATA[Folosind diverse resurse online (inclusiv lista de User-Agents folositi de dispozitivele mobile de aici), am scris o mica clasa PHP ce detecteaza platforma mobila a utilizatorului din cele mai cunoscute (fara nici o pretentie de exhaustivitate): Google Android BlackBerry iPhone Opera Mini Palm Windows Mobile Generic (adica restul) Modul de utilizare este foarte simplu, descris [...]


<b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/php-class-autoloader/' rel='bookmark' title='Permanent Link: PHP Class: AutoLoader'>PHP Class: AutoLoader</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Folosind diverse resurse online (inclusiv lista de User-Agents folositi de dispozitivele mobile de <a href="http://www.zytrax.com/tech/web/mobile_ids.html" title="Mobile ID Strings" target="_blank">aici</a>), am scris o mica clasa PHP ce detecteaza platforma mobila a utilizatorului din cele mai cunoscute (fara nici o pretentie de exhaustivitate):</p>
<ul>
<li>Google Android</li>
<li>BlackBerry</li>
<li>iPhone</li>
<li>Opera Mini</li>
<li>Palm</li>
<li>Windows Mobile</li>
<li>Generic (adica restul)</li>
</ul>
<p>Modul de utilizare este foarte simplu, descris in detaliu pe <a href="http://code.google.com/p/php-mobile-detect/" title="Google Code"><b>Google Code</b></a></p>
<p>Nu am avut foarte multe device-uri la indemana pentru teste (decat cateva standard, plus un iPhone si un Blackberry), asa ca as aprecia daca m-ati atentiona daca nu functioneaza cum ar trebui.</p>


<p><b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/php-class-autoloader/' rel='bookmark' title='Permanent Link: PHP Class: AutoLoader'>PHP Class: AutoLoader</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.victorstanciu.ro/detectarea-platformelor-mobile-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FTL jump &#8230; now!</title>
		<link>http://www.victorstanciu.ro/ftl-jump/</link>
		<comments>http://www.victorstanciu.ro/ftl-jump/#comments</comments>
		<pubDate>Tue, 19 May 2009 17:23:12 +0000</pubDate>
		<dc:creator>Victor Stanciu</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.victorstanciu.ro/?p=284</guid>
		<description><![CDATA[Fiecare fisier JavaScript, CSS, imagine, Flash, etc. afisat utilizatorului reprezinta un request catre server. Pentru fiecare asemenea fisier browserul trimite cererea, serverul o preia, proceseaza, serveste fisierul cerut, si tot asa. La site-urile &#8220;mari&#8221; se simte. In load-ul serverului, in latimea de banda consumata, in experienta utilizatorilor. Urmand recomandarile echipei de la Yahoo!, o sa [...]


<b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/' rel='bookmark' title='Permanent Link: PHP SoapClient port bug workaround'>PHP SoapClient port bug workaround</a></li><li><a href='http://www.victorstanciu.ro/php-class-autoloader/' rel='bookmark' title='Permanent Link: PHP Class: AutoLoader'>PHP Class: AutoLoader</a></li><li><a href='http://www.victorstanciu.ro/ajaj-autocompleter/' rel='bookmark' title='Permanent Link: AJAJ Autocompleter'>AJAJ Autocompleter</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Fiecare fisier JavaScript, CSS, imagine, Flash, etc. afisat utilizatorului reprezinta un request catre server. Pentru fiecare asemenea fisier browserul trimite cererea, serverul o preia, proceseaza, serveste fisierul cerut, si tot asa. La site-urile &#8220;mari&#8221; se simte. In load-ul serverului, in latimea de banda consumata, in experienta utilizatorilor.</p>
<p>Urmand <a href="http://developer.yahoo.com/performance/rules.html" title="<br />
Best Practices for Speeding Up Your Web Site" target="_blank">recomandarile</a> echipei de la Yahoo!, o sa va arat la ce solutie am ajuns.</p>
<p>Dar mai intai &#8230; cifre. Am ales <a href="http://www.okazii.ro/" title="Okazii" target="_blank"><b>Okazii.ro</b></a> drept studiu de caz (toate masuratorile sunt facute cu <a href="http://developer.yahoo.com/yslow/" title="YSlow" target="_blank">YSlow</a>, folosind o versiune salvata a primei pagini):</p>
<h2>Fara <a href="http://en.wikipedia.org/wiki/FTL_(Battlestar_Galactica)" title="FTL" target="_blank">FTL</a> (pagina <a href="http://dev.victorstanciu.ro/experimente/okazii/old/" title="Okazii.ro - prima pagina">salvata aici</a>)</h2>
<ul>
<li>Nota generala in YSlow: <b>E</b></li>
<li>Numar request-uri pe prima pagina: <b>62</b></li>
<li>Dimensiune prima pagina: <b>406.4KB</b></li>
</ul>
<h2>Cu FTL  (pagina <a href="http://dev.victorstanciu.ro/experimente/okazii/" title="Okazii.ro - prima pagina">salvata aici</a>)</h2>
<ul>
<li>Nota generala in YSlow: <b>C</b></li>
<li>Numar request-uri pe prima pagina: <b>51</b></li>
<li>Dimensiune prima pagina: <b>226.0KB</b></li>
</ul>
<p>Calculat la traficul lor (aprox. <b>82.000</b> vizitatori unici <b>pe zi</b>), netinand cont de cache in nici unul din cazuri, asta se traduce in o diferenta de <b>902.000</b> de request-uri si <b>14.1 GB</b> trafic <b>lunar</b>.</p>
<h2>Cum se foloseste</h2>
<p>Presupunand urmatoarea structura de directoare:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/public
    /css
    /js
index.php</pre></div></div>

<p><a href="http://dev.victorstanciu.ro/ftl/ftl.zip" title="Download"><b>Descarcati arhiva</b></a> si extrageti fisierele in root asa incat noua structura va fi:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/ftl
    /cache
    /lib
    CSS.php
    File.php
    JS.php
    config.php
    index_css.php
    index_js.php
/public
    /css
    /js
index.php</pre></div></div>

<p>Mutati fisierele index_css.php si index_js.php din directorul /ftl in directorul /public/css, respectiv /public/js, si redenumiti-le in index.php:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/ftl
    /cache
    /lib
    CSS.php
    File.php
    JS.php
    config.php
/public
    /css
        index.php
    /js
        index.php
index.php</pre></div></div>

<p>Adaugati liniile urmatoare in .htaccess:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">&lt;IfModule mod_expires.c&gt;
    ExpiresActive On
    ExpiresByType application/x-javascript A315360000
    ExpiresByType text/css A315360000
&lt;/IfModule&gt;
&nbsp;
RewriteEngine On
RewriteRule ^public/(js|css)/([a-z0-9\.]+)\.(js|css)$    public/$1/index.php?hash=$2</pre></td></tr></table></div>

<p>Includeti fisierele necesare in index.php:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</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: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ftl/JS.php&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: #0000ff;">&quot;ftl/CSS.php&quot;</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>Totul e pregatit, tot ce mai trebuie sa facem e sa incarcam in pagina fisierele. Spre exemplu, ca sa incarc in &lt;head&gt; cateva fisiere CSS si JS:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</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: #000088;">$css</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FTL_CSS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$js</span>  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FTL_JS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$css</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'reset'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'typo'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ie'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lte IE 7'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prototype'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sizzle'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'effects'</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lightbox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$css</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><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;">$js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</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>Nu uitati sa setati drepturi de scriere pe subdirectoarele din directorul /ftl/cache.</p>
<p>Codul HTML generat va arata in felul urmator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;public/css/1e9dd525d1fbe94f7b8aede22cc141f5.1242750618.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if lte IE 7]&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;public/css/25400724d7370b0b29c9369d9af3dd21.1242750626.css&quot; /&gt;&lt;![endif]--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;public/js/56c728a5a7689ab1663359a7edff160a.1242751025.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>Doua fisiere .css (unul pentru cele standard, unul incarcat doar pentru IE), si un fisier .js. <b>3 request</b>-uri in loc de <b>9</b>, si o dimensiune totala de aprox. <b>6.5 ori mai mica</b>. Fisierele sunt atat <b>minified</b> cat si <b>gzipped</b>, folosind librariile <a href="http://csstidy.sourceforge.net/" title="CSSTidy">CSSTidy</a>, respectiv <a href="http://dean.edwards.name/packer/" title="Packer">Packer</a></p>
<h2>Suna bine, dar ce se intampla cand &#8230;</h2>
<ul>
<li>
Modific sau sterg un fisier .css sau .js ? Trebuie sa schimb ceva de fiecare data ?</p>
<p><i>Nu. De fiecare data cand un fisier este modificat sau sters, fisierul rezultat este regenerat <b>automat</b>, fara nevoie de interventie.</i></p>
</li>
<li>
Un utilizator foloseste un browser ce nu suporta Gzip (stone-age style) ?</p>
<p><i>Respectivului utilizator ii este servita <b>automat</b> o versiune doar <b>minified</b>. Putin mai mare, evident, dar functionala.</i></p>
</li>
<li>
Structura directoarelor mele nu corespunde cu cea din exemplul de mai sus ?<br />
<i>In directorul /ftl este un fisier config.php unde puteti schimba caile catre fisiere / directoare. Aveti grija numai ca include-urile sa functioneze.</i>
</li>
</ul>


<p><b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/' rel='bookmark' title='Permanent Link: PHP SoapClient port bug workaround'>PHP SoapClient port bug workaround</a></li><li><a href='http://www.victorstanciu.ro/php-class-autoloader/' rel='bookmark' title='Permanent Link: PHP Class: AutoLoader'>PHP Class: AutoLoader</a></li><li><a href='http://www.victorstanciu.ro/ajaj-autocompleter/' rel='bookmark' title='Permanent Link: AJAJ Autocompleter'>AJAJ Autocompleter</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.victorstanciu.ro/ftl-jump/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PHP Class: AutoLoader</title>
		<link>http://www.victorstanciu.ro/php-class-autoloader/</link>
		<comments>http://www.victorstanciu.ro/php-class-autoloader/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 20:00:38 +0000</pubDate>
		<dc:creator>Victor Stanciu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[class]]></category>

		<guid isPermaLink="false">http://www.victorstanciu.ro/?p=26</guid>
		<description><![CDATA[Oricine a adoptat metodologia OOP in lucrul cu PHP cu siguranta s-a bucurat la implementarea functiei __autoload in PHP 5, ce permite incarcarea automata a fisierului corespunzator unei clase la momentul invocarii acesteia. Probleme In ciuda potentialului imens adus de aceasta functionalitate, majoritatea cartilor de specialitate prezinta doar cel mai elementar exemplu pentru exploatarea acestei [...]


<b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/ftl-jump/' rel='bookmark' title='Permanent Link: FTL jump &#8230; now!'>FTL jump &#8230; now!</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Oricine a adoptat metodologia OOP in lucrul cu PHP cu siguranta s-a bucurat la implementarea functiei <a href="http://www.php.net/autoload" title="Autoload">__autoload</a> in PHP 5, ce permite incarcarea automata a fisierului corespunzator unei clase la momentul invocarii acesteia.</p>
<h2>Probleme</h2>
<p>In ciuda potentialului imens adus de aceasta functionalitate, majoritatea cartilor de specialitate prezinta doar cel mai elementar exemplu pentru exploatarea acestei functii, si anume simpla incarcare a fisierului cu numele identic sau asemanator cu cel al clasei necesare:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><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: #b1b100;">require_once</span> <span style="color: #000088;">$class_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Imediat dupa trecerea entuziasmului, incepem sa observam <b>minusurile</b> acestei abordari:</p>
<ul>
<li>Necesitatea ca fisierele si clasele corespunzatoare sa fie denumite la fel.</li>
<li>Toate fisierele vor trebui sa fie situate in acelasi director, pentru a putea fi incluse.</li>
<li>Fiecare fisier va putea contine o singura clasa. (Cu toate ca si eu consider &#8220;best-practice&#8221; pastrarea cate unui singur fisier per clasa, prefer sa stiu ca nu sunt constrans in aceasta directie).</li>
</ul>
<h2>Cerinte</h2>
<p>Una din marile frumuseti ale programarii este ca permite transpunerea logicii gandirii umane intr-un limbaj &#8220;inteles&#8221; de catre tehnologiile disponibile. La fel este si cazul procesului decizional ce urmeaza:</p>
<ul>
<li>Avem nevoie de un sistem de localizare si incarcare a claselor care sa fie independent de structura directoarelor aplicatiei, si care sa rezolve punctele enumerate mai sus.</li>
<li>Va trebui ca solutia adoptata sa poata depista clasele in anumite directoare alese, in acest fel asigurandu-se incarcarea unui fisier necesar, din locatia deja cunoscuta a acestuia.</li>
<li>Pentru a evita solicitarea excesiva a serverului, este necesar ca sistemul sa permita o memorare (cache) a locatiei fiecarei clase, asa incat parcurgerea directoarelor sa se faca o singura data.</li>
</ul>
<h2>Solutie</h2>
<p>Dupa ce a trecut prin mai multe iteratii si versiuni (si probabil va mai trece), va invit sa descarcati si folositi versiunea actuala a clasei <i>AutoLoader</i>. Modul de utilizare este simplu, doar incarcati fisierul in care se afla aceasta,  si folositi urmatoarea varianta a functiei <b>__autoload</b>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$class_dirs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// array ce va contine directoarele in care vom cauta (inclusiv subdirectoarele acestora, recursiv)</span>
	<span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AutoLoader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_dirs</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'autoloader-cache.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	try <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$autoloader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></td></tr></table></div>

<p>Puteti vedea output-ul unui scurt exemplu <a href="http://dev.victorstanciu.ro/experimente/php-class-autoloader/" title="PHP Class: AutoLoader - exemplu">aici</a>, ce contine urmatorul cod PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AutoLoader.class.php&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: #0000ff;">&quot;autoload.func.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">new</span> Test<span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Avantaje</h2>
<ul>
<li>Incarcarea explicita a unei <b>singure</b> clase, cea de autoload.</li>
<li>Parcurgerea <b>recursiva</b> a directoarelor indicate si pastrarea locatiei fiecarei clase intr-un fisier de cache, pentru acces rapid.</li>
<li>Regenerarea <b>automata</b> a acestui fisier pe parcursul dezvoltarii. In momentul adaugarii / modificartii unei clase, locatia acesteia negasindu-se in fisierul existent, acesta este recreat.</li>
<li>Inexistenta restrictiei privitoare la legatura intre numele unei clase si denumirea fisierului in care aceasta se afla.</li>
</ul>
<h2>Dezavantaje</h2>
<p>Unul din dezavantajele minore ale acestui sistem este imposibilitatea existentei a doua clase cu acelasi nume. In cazul in care numele a doua clase coincid, locatia primei intalnite la scanarea fisierelor va fi suprascrisa in fisierul de cache de catre cea de-a doua.</p>
<p>Cu toate ca in mod normal nu ar trebui sa existe doua clase denumite la fel, se impune in acest fel o atentie sporita in momentul utilizarilor de clase / plugin-uri &#8220;3rd party&#8221;, deoarece numele uneia din acestea poate coincide cu cel al unei clase deja existente.</p>
<h2>Download</h2>
<p>Puteti downloada atat clasa <i>AutoLoader</i>, cat si toate fisierele corespunzatoare exemplului anterior:</p>
<p><a href="http://dev.victorstanciu.ro/experimente/php-class-autoloader/AutoLoader.class.zip" title="Download AutoLoader" class="highlight">Download AutoLoader.class.php</a><br />
<a href="http://dev.victorstanciu.ro/experimente/php-class-autoloader/autoloader.zip" title="Download exemplu" class="highlight">Download exemplu</a></p>
<p>Ca de obicei, sunt foarte interesat de eventualele imbunatatiri / modificari pe care le-ati propune. De asemenea, daca depistati bug-uri, as fi recunoscator daca mi le-ati indica.</p>


<p><b>Related posts</b>:<ol><li><a href='http://www.victorstanciu.ro/ftl-jump/' rel='bookmark' title='Permanent Link: FTL jump &#8230; now!'>FTL jump &#8230; now!</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.victorstanciu.ro/php-class-autoloader/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
