It’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 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.
You can think of a namespace as a container of other entities (including namespaces); you declare a namespace using one of two different syntaxes:
01.namespace MyAwesomeProject;02. 03.const A_CONSTANT = 'not awesome';04.function awesomeFunction() {05. echo "This function is awesome";06.}07. 08.namespace StillAwesome {09. const A_CONSTANT = 'awesome';10. 11. function awesomeFunction() {12. echo "This function is still " .13.StillAwesome\A_CONSTANT;14. }15.}You can now access the entities that belong to either of these by using the namespace operator ‘\’:
1.require "awesome-file.php";2. 3.MyAwesomeProject\awesomeFunction();4.StillAwesome\awesomeFunction();What if you don’t use namespaces? According to the official FAQ, 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.
2. Closures
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.
Consider, for example, this simple snippet of code:
01.function loadData($url, $handler) {02. $handler(file_get_contents($url));03.}04. 05.function sendPageTo($email, $url) {06. loadData($url, function($data) use ($email) {07. mail($email, "Here's your data", $data);08. });09.}As you can see, the loadData method takes in a lambda function as a variable, and then calls it with the data retrieved from the URL passed to it. In our case, sendPageTo() creates a closure by assigning the $email parameter from its scope into the function passed to loadData(), which then uses it to e-mail the result of loadData() to a given recipient.
3.Garbage Collection
A garbage collector essentially cleans up unused memory. The new garbage collection mechanism available in PHP (via the zend.enable_gc php.ini 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.
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.
4. GOTO
Yes, you read correctly: GOTO has made it into PHP. We’ve already covered it in a recent story, so go read it if you haven’t already before you start tearing your hair out and crying foul.
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’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’s not the same thing.
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’t jump into the middle of another function). For example:
01.function fussyRandom() {02. for ($i = 0; $i < 1000; $i++) {03. if (rand(0, 10000) > 9000) {04. goto error;05. }06. }07. 08. return;09. 10.error:11. die("I can't stand random numbers greater than 9,000!\n");12.}13. 14.fussyRandom();In this simple script, if the random number generated inside the fussyRandom function is greater than 9,000, the script will be bounced to the first instruction after the error label.
The fileinfo extension
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’s available, like a file upload), but that’s not a reliable method of identification.
Or, you could use the fileinfo 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 contents of a file and determining its type by using a heuristic approach. Fileinfo is not perfect, but it is very good and gets the type of a given file right almost every time.
Using this extension is very easy:
01.$finfo = new finfo(FILEINFO_MIME);02. 03.//Local file04.//Returns text/plain; charset=us-ascii (on Unices)05.echo $finfo->file('/etc/hosts');06. 07.//Remote file (via string)08.//Returns text/html; charset=utf-809.echo $finfo->buffer(file_get_contents('http://phparch.com')); 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.
Related posts:

