<?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>WEB-NES-BAY &#187; Conflicts</title>
	<atom:link href="http://webnesbay.com/tag/conflicts/feed/" rel="self" type="application/rss+xml" />
	<link>http://webnesbay.com</link>
	<description>Learn Tips and tricks on Linux, Hacking, linux, PHP, Perl, Web, Hardware</description>
	<lastBuildDate>Sun, 11 Apr 2010 05:12:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP 5.3 Released: New features</title>
		<link>http://webnesbay.com/php-5-3-released-new-features/</link>
		<comments>http://webnesbay.com/php-5-3-released-new-features/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:26:12 +0000</pubDate>
		<dc:creator>WEBNESBAY</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Closure]]></category>
		<category><![CDATA[Conflicts]]></category>
		<category><![CDATA[Entities]]></category>
		<category><![CDATA[File Php]]></category>
		<category><![CDATA[Fileinfo]]></category>
		<category><![CDATA[First Class]]></category>
		<category><![CDATA[Free Variables]]></category>
		<category><![CDATA[Garbage Collector]]></category>
		<category><![CDATA[Goto]]></category>
		<category><![CDATA[Independent Packages]]></category>
		<category><![CDATA[Legacy Code]]></category>
		<category><![CDATA[Namespace]]></category>
		<category><![CDATA[New Features]]></category>
		<category><![CDATA[One More Reason]]></category>
		<category><![CDATA[Phar]]></category>
		<category><![CDATA[Php 4]]></category>
		<category><![CDATA[Quite Some Time]]></category>
		<category><![CDATA[Scope]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[View Source]]></category>

		<guid isPermaLink="false">http://webnesbay.com/?p=235</guid>
		<description><![CDATA[It&#8217;s official: PHP 5.3 is  out and  available for download. “5.3 comes with a whole load of new syntax, closures, namespaces and, last but not least, goto. Besides the cool new syntax items, there are also two new extensions,  phar and  fileinfo. What is most exciting is a proper garbage collector, as this makes PHP running as a daemon (or other long running) process finally feasible.
1. Namespaces
Namespaces have been keeping the internals mailing list busy for [...]


Related posts:<ol><li><a href='http://webnesbay.com/ubuntu-10-04-best-features/' rel='bookmark' title='Permanent Link: Ubuntu 10.04 Best features'>Ubuntu 10.04 Best features</a></li>
<li><a href='http://webnesbay.com/opera-10-0-released-with-integrated-web-server-functionality/' rel='bookmark' title='Permanent Link: Opera 10.0 Released, With Integrated Web Server Functionality'>Opera 10.0 Released, With Integrated Web Server Functionality</a></li>
<li><a href='http://webnesbay.com/create-pdf-using-perl/' rel='bookmark' title='Permanent Link: Create PDF using perl'>Create PDF using perl</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s official: PHP 5.3 is  <a href="http://www.php.net/archive/2009.php#id2009-06-30-1">out</a> and  <a href="http://www.php.net/downloads.php">available for download</a>. “5.3 comes with a whole load of new syntax, closures, namespaces and, last but not least, goto. Besides the cool new syntax items, there are also two new extensions,  <a href="http://ca.php.net/phar">phar</a> and  <a href="http://ca2.php.net/fileinfo">fileinfo</a>. What is most exciting is a proper garbage collector, as this makes PHP running as a daemon (or other long running) process finally feasible.</p>
<h2>1. Namespaces</h2>
<p>Namespaces have been keeping the internals mailing list busy for quite some time—and they have been one of the most requested and discussed feature for at least two or three years. Now that they are finally available, they will make it easier to use multiple independent packages within your applications without causing naming conflicts.</p>
<p>You can think of a namespace as a container of other entities (including namespaces); you declare a namespace using one of two different syntaxes:</p>
<div id="highlighter_864139">
<div>
<div><a style="width: 16px; height: 16px;" title="view source" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#viewSource">view source</a></p>
<div><embed id="highlighter_864139_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.phparch.com/syntaxhighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_864139" menu="false"></embed></div>
<p><a style="width: 16px; height: 16px;" title="print" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#printSource">print</a><a style="width: 16px; height: 16px;" title="?" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#about">?</a></div>
</div>
<div>
<div><code>01.</code><span><span style="margin-left: 0px ! important;"><code>namespace MyAwesomeProject;</code></span></span></div>
<div><code>02.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>03.</code><span><span style="margin-left: 0px ! important;"><code>const</code> <code>A_CONSTANT = </code><code>'not awesome'</code><code>;</code></span></span></div>
<div><code>04.</code><span><span style="margin-left: 0px ! important;"><code>function</code> <code>awesomeFunction() {</code></span></span></div>
<div><code>05.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>echo</code> <code>"This function is awesome"</code><code>;</code></span></span></div>
<div><code>06.</code><span><span style="margin-left: 0px ! important;"><code>}</code></span></span></div>
<div><code>07.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>08.</code><span><span style="margin-left: 0px ! important;"><code>namespace StillAwesome {</code></span></span></div>
<div><code>09.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>const</code> <code>A_CONSTANT = </code><code>'awesome'</code><code>;</code></span></span></div>
<div><code>10.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>11.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>function</code> <code>awesomeFunction() {</code></span></span></div>
<div><code>12.</code><span><code> </code><span style="margin-left: 48px ! important;"><code>echo</code> <code>"This function is still "</code> <code>.</code></span></span></div>
<div><code>13.</code><span><span style="margin-left: 0px ! important;"><code>StillAwesome\A_CONSTANT;</code></span></span></div>
<div><code>14.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>}</code></span></span></div>
<div><code>15.</code><span><span style="margin-left: 0px ! important;"><code>}</code></span></span></div>
</div>
</div>
<p>You can now access the entities that belong to either of these by using the namespace operator &#8216;\&#8217;:</p>
<div id="highlighter_345770">
<div>
<div><a style="width: 16px; height: 16px;" title="view source" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#viewSource">view source</a></p>
<div><embed id="highlighter_345770_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.phparch.com/syntaxhighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_345770" menu="false"></embed></div>
<p><a style="width: 16px; height: 16px;" title="print" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#printSource">print</a><a style="width: 16px; height: 16px;" title="?" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#about">?</a></div>
</div>
<div>
<div><code>1.</code><span><span style="margin-left: 0px ! important;"><code>require</code> <code>"awesome-file.php"</code><code>;</code></span></span></div>
<div><code>2.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>3.</code><span><span style="margin-left: 0px ! important;"><code>MyAwesomeProject\awesomeFunction();</code></span></span></div>
<div><code>4.</code><span><span style="margin-left: 0px ! important;"><code>StillAwesome\awesomeFunction();</code></span></span></div>
</div>
</div>
<p>What if you don&#8217;t use namespaces? According to the  <a href="http://ca2.php.net/manual/en/language.namespaces.faq.php">official FAQ</a>, you can just ignore them. Thus, your legacy code should continue working just fine—one more reason to consider a switch to 5.3 today.</p>
<h2>2. Closures</h2>
<p>Closures are functions whose scope is partially defined by one or more “free” variables that only become bound at runtime. You have probably already encountered closures in JavaScript, where they are often used to handle events. A closure allows you to use a first-class function with one or more parameters inherited from a scope other than its own natural scope.</p>
<p>Consider, for example, this simple snippet of code:</p>
<div id="highlighter_64947">
<div>
<div><a style="width: 16px; height: 16px;" title="view source" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#viewSource">view source</a></p>
<div><embed id="highlighter_64947_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.phparch.com/syntaxhighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_64947" menu="false"></embed></div>
<p><a style="width: 16px; height: 16px;" title="print" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#printSource">print</a><a style="width: 16px; height: 16px;" title="?" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#about">?</a></div>
</div>
<div>
<div><code>01.</code><span><span style="margin-left: 0px ! important;"><code>function</code> <code>loadData(</code><code>$url</code><code>, </code><code>$handler</code><code>) {</code></span></span></div>
<div><code>02.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>$handler</code><code>(</code><code>file_get_contents</code><code>(</code><code>$url</code><code>));</code></span></span></div>
<div><code>03.</code><span><span style="margin-left: 0px ! important;"><code>}</code></span></span></div>
<div><code>04.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>05.</code><span><span style="margin-left: 0px ! important;"><code>function</code> <code>sendPageTo(</code><code>$email</code><code>, </code><code>$url</code><code>) {</code></span></span></div>
<div><code>06.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>loadData(</code><code>$url</code><code>, </code><code>function</code><code>(</code><code>$data</code><code>) </code><code>use</code> <code>(</code><code>$email</code><code>) {</code></span></span></div>
<div><code>07.</code><span><code> </code><span style="margin-left: 48px ! important;"><code>mail(</code><code>$email</code><code>, </code><code>"Here's your data"</code><code>, </code><code>$data</code><code>);</code></span></span></div>
<div><code>08.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>});</code></span></span></div>
<div><code>09.</code><span><span style="margin-left: 0px ! important;"><code>}</code></span></span></div>
</div>
</div>
<p>As you can see, the loadData method takes in a  <em>lambda function</em> as a variable, and then calls it with the data retrieved from the URL passed to it. In our case,  <em>sendPageTo()</em> creates a closure by assigning the  <em>$email</em> parameter from its scope into the function passed to  <em>loadData()</em>, which then uses it to e-mail the result of  <em>loadData()</em> to a given recipient.</p>
<h2>3.Garbage Collection</h2>
<p>A  <em>garbage collector</em> essentially cleans up unused memory. The new garbage collection mechanism available in PHP (via the  <em>zend.enable_gc</em> <a href="http://php.net/gc_enable">php.ini</a> setting) helps in situations where a cyclic reference between two data elements prevents either of them from being freed, thus leading to an increasing waste of memory.</p>
<p>Under most circumstances, this is not a problem, as most PHP scripts are short-lived. In some cases—for example, complex, long-running scripts or daemons—the use of the garbage collector can optimize memory usage, albeit at the cost of the CPU cycles it needs to run.</p>
<h2>4. GOTO</h2>
<p>Yes, you read correctly: GOTO has made it into PHP. We&#8217;ve already covered it  <a href="http://phparch.com/main/news/view/8/GOTO_in_PHP_5_3__is_it_Really_That_Evil_"> in a recent story</a>, so go read it if you haven&#8217;t already before you start tearing your hair out and crying foul.</p>
<p>The reality is, there are legitimate reasons for using GOTO in some types of PHP scripts—which, of course, means that there are plenty of illegitimate reasons to use it, too. For example, if you have ever written finite-state automata, you&#8217;ll know that a well-placed GOTO statement makes it relatively easy to manage nested error conditions. Sure—you could do the same using Exceptions or if-then-else statements, but, speaking from experience, we can safely say that it&#8217;s not the same thing.</p>
<p>You can use GOTO to cause the execution flow to unconditionally jump to a specific location, as long as it is within the current scope (thus, you can&#8217;t jump into the middle of another function). For example:</p>
<div id="highlighter_384441">
<div>
<div><a style="width: 16px; height: 16px;" title="view source" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#viewSource">view source</a></p>
<div><embed id="highlighter_384441_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.phparch.com/syntaxhighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_384441" menu="false"></embed></div>
<p><a style="width: 16px; height: 16px;" title="print" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#printSource">print</a><a style="width: 16px; height: 16px;" title="?" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#about">?</a></div>
</div>
<div>
<div><code>01.</code><span><span style="margin-left: 0px ! important;"><code>function</code> <code>fussyRandom() {</code></span></span></div>
<div><code>02.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>for</code> <code>(</code><code>$i</code> <code>= 0; </code><code>$i</code> <code>&lt; 1000; </code><code>$i</code><code>++) {</code></span></span></div>
<div><code>03.</code><span><code> </code><span style="margin-left: 48px ! important;"><code>if</code> <code>(rand(0, 10000) &gt; 9000) {</code></span></span></div>
<div><code>04.</code><span><code> </code><span style="margin-left: 72px ! important;"><code>goto error;</code></span></span></div>
<div><code>05.</code><span><code> </code><span style="margin-left: 48px ! important;"><code>}</code></span></span></div>
<div><code>06.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>}</code></span></span></div>
<div><code>07.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>08.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>return</code><code>;</code></span></span></div>
<div><code>09.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>10.</code><span><span style="margin-left: 0px ! important;"><code>error:</code></span></span></div>
<div><code>11.</code><span><code> </code><span style="margin-left: 24px ! important;"><code>die</code><code>(</code><code>"I can't stand random numbers greater than 9,000!\n"</code><code>);</code></span></span></div>
<div><code>12.</code><span><span style="margin-left: 0px ! important;"><code>}</code></span></span></div>
<div><code>13.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>14.</code><span><span style="margin-left: 0px ! important;"><code>fussyRandom();</code></span></span></div>
</div>
</div>
<p>In this simple script, if the random number generated inside the  <em>fussyRandom</em> function is greater than 9,000, the script will be bounced to the first instruction after the  <em>error</em> label.</p>
<h2>The  <em>fileinfo</em> extension</h2>
<p>Understanding the contents of a file is no easy task. You could rely on the extension of its name—but that would make tricking you into misinterpreting a malicious data stream all too easy. You could trust the MIME type sent by a user agent (if you happen to be working in an environment where that&#8217;s available, like a file upload), but that&#8217;s not a reliable method of identification.</p>
<p>Or, you could use the  <em>fileinfo</em> library, which has been available in UNIX systems for many years and has finally landed in PHP 5.3. This library, and its eponymous command-line utility, works by examining the  <em>contents</em> of a file and determining its type by using a heuristic approach. Fileinfo is not perfect, but it is  <em>very</em> good and gets the type of a given file right almost every time.</p>
<p>Using this extension is very easy:</p>
<div id="highlighter_399372">
<div>
<div><a style="width: 16px; height: 16px;" title="view source" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#viewSource">view source</a></p>
<div><embed id="highlighter_399372_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://www.phparch.com/syntaxhighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_399372" menu="false"></embed></div>
<p><a style="width: 16px; height: 16px;" title="print" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#printSource">print</a><a style="width: 16px; height: 16px;" title="?" href="http://www.phparch.com/main/news/view/29/Poll__Five_PHP_5_3_Reasons_to_Make_You_Switch#about">?</a></div>
</div>
<div>
<div><code>01.</code><span><span style="margin-left: 0px ! important;"><code>$finfo</code> <code>= </code><code>new</code> <code>finfo(FILEINFO_MIME);</code></span></span></div>
<div><code>02.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>03.</code><span><span style="margin-left: 0px ! important;"><code>//Local file</code></span></span></div>
<div><code>04.</code><span><span style="margin-left: 0px ! important;"><code>//Returns text/plain; charset=us-ascii (on Unices)</code></span></span></div>
<div><code>05.</code><span><span style="margin-left: 0px ! important;"><code>echo</code> <code>$finfo</code><code>-&gt;file(</code><code>'/etc/hosts'</code><code>);</code></span></span></div>
<div><code>06.</code><span><span style="margin-left: 0px ! important;"> </span></span></div>
<div><code>07.</code><span><span style="margin-left: 0px ! important;"><code>//Remote file (via string)</code></span></span></div>
<div><code>08.</code><span><span style="margin-left: 0px ! important;"><code>//Returns text/html; charset=utf-8</code></span></span></div>
<div><code>09.</code><span><span style="margin-left: 0px ! important;"><code>echo</code> <code>$finfo</code><code>-&gt;buffer(</code><code>file_get_contents</code><code>(</code><code>'<a href="http://phparch.com/">http://phparch.com</a>'</code><code>)); </code></span></span></div>
</div>
</div>
<p>As you can see, fileinfo is capable of identifying content either by examining a local file, or by examining the contents of a string buffer—which makes it a handy tool for dynamically-loaded data.</p>


<p>Related posts:<ol><li><a href='http://webnesbay.com/ubuntu-10-04-best-features/' rel='bookmark' title='Permanent Link: Ubuntu 10.04 Best features'>Ubuntu 10.04 Best features</a></li>
<li><a href='http://webnesbay.com/opera-10-0-released-with-integrated-web-server-functionality/' rel='bookmark' title='Permanent Link: Opera 10.0 Released, With Integrated Web Server Functionality'>Opera 10.0 Released, With Integrated Web Server Functionality</a></li>
<li><a href='http://webnesbay.com/create-pdf-using-perl/' rel='bookmark' title='Permanent Link: Create PDF using perl'>Create PDF using perl</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://webnesbay.com/php-5-3-released-new-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
