Article
PHP5: Coming Soon to a Webserver Near You
That's not all folks...
Now if all this wasn't enough, there are some other important new features that are either already available, or are coming soon.
Streams
Wez Furlong has done an outstanding job to unify essentially all PHP IO operations around the Streams API. Some of this is already available with PHP4; for example you can use the file function fopen() to connect to a remote Website like this:
fopen('http://www.sitepoint.com','r');
At an engine level, practically all IO related functionality within PHP5 should be unified under Streams, which means things like:
fopen('ftp://www.ftpserver.com','r');
are very possible.
What's more, it offers some very interesting possibilities for those PHP developers implementing clients for other network protocols (such as the mail protocol SMTP), as suggested by the stream_wrapper_register() function.
Also interesting is how the Streams API provides an alternative to the socket functions, which can be a great help if you're planning to write some kind of stand alone (from Apache, that is) PHP server, as demonstrated here.
A full presentation on Streams is available here.
SPL
SPL stands for the Standard PHP Library, and for OOP enthusiasts, this is certainly a fascinating prospect. On the label, it's described as an extension that "allows you to register a set of standard engine level interfaces."
In practice, what it seems to mean is that you'll be able to write classes in PHP which become available for use via the native PHP constructs, as an alternative to requiring users work with the API you've defined.
Right now, the focus of the SPL extension has been to make it possible to implement Iterators, which become available for use via PHP's foreach() construct.
That may not sound too interesting, but for anyone who's struggled with "binding" a result from a database to a class that, say, renders an HTML table, this will likely put a very big grin on your face.
The SPL extension wasn't available with the Beta (unless I missed something), but in theory using it might look something like this:
<?php
// Note: doesn't work with the PHP5 beta
class Colors implements spl_iterator {
var $collection;
function __construct() {
$this->collection = array ('red','blue','green');
}
function new_iterator() {
return new ColorIterator($this);
}
}
class ColorIterator implements spl_forward {
private $colors;
private $pos = 0;
private $max;
function __construct($colors) {
$this->colors = $colors;
$this->max = count ( $this->colors->collection );
}
function current() {
return $this->colors->collection[$this->pos];
}
function next() {
$this->pos++;
}
function has_more() {
return $this->pos < $this->max;
}
}
$colors = new Colors();
foreach($colors as $color) {
echo ( $color.'<br />' );
}
?>
In other words, the code that's using the collection (the collection being the Colors class in above example) no longer has to care about knowing the correct iterator methods required to fetch the data. Personally, I think that's a big step forward for code re-use in PHP.
Given the name of this extension, it would seem that the authors’ plan is not to stop at iterators. Perhaps we'll see other well known design patterns that are often implemented within the class libraries of languages like Java, such as the Observer pattern, making their way in here.