Jul 23: Unicode identifiers

When I see people talking about Unicode and PHP 6 I often see them mentioning one fact as a big change: PHP 6 allows (mostly any) arbitrary Unicode character as (part of an) identifier. So you can have code like this:
function 新日本石油() {
   echo "Let's hope this isn't an offensive function name... ";
   echo "it's copied from some news site";
}
新日本石油();
Well yes, that's funny, at first but serves a purpose: Consider you have an application tied to an environment with a special terminology, then translating this terms to English might be extremely confusing (especially as programmers often don't really know the correct terminology of that domain) and it's good to call the thing by it's name - while that can be quite complicated, too, in a previous job we had such a case and often used the German terms which produced quite funny names for getters and setters which didn't satisfy us ... but that's not what I wanted to talk about.
The purpose of this were some bad news: That's nothing new. The relevant scanner rule hasn't changed since 4.0 - the only change is that PHP 6 doesn't treat it as random set of bytes anymore but knows about Unicode codepoints and interprets is as such.
Out of interest I did some little digging into the PHP repository's history:
$ svn annotate trunk/Zend/zend_language_scanner.l
...
34779 zeev LABEL [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
...
$ svn log -r 34779
------------------------------------------------------------------------
r34779 | zeev | 2000-10-29 15:35:34 +0100 (Sun, 29 Oct 2000) | 2 lines
Unify the names of these last 3 files...
------------------------------------------------------------------------
The result? - The rule, as it is in PHP 6 wasn't changed since 2000, really nothing new there with PHP 6, and even then the only change was that the scanner file was renamed...
Jul 14: PHP 5.3 Release Party

In my blog post for the 5.3 release I already mentioned it and some others posted it, too: We're planning a release party in Munich, currently we have around 60 people who registered. That's the official announcement:
We like to invite you to the PHP 5.3 release party on Friday, the 17th of July in Munich. The release party offers a chance to talk to other PHP enthusiasts and enjoy that PHP is alive and kicking. If you just can't resist a decent barbecue together with some beer and other drinks you are also welcomed.The happening will take place at the Waldwirtschaft beer garden irrespective of the weather. We will meet at 19:00 o'clock - open end.
The location is famous for its huge beer garden (2,500 available seats, a children’s playground) and its typical Bavarian but also international food. On sunny weather you may even enjoy live-music and listen to the sounds of Jazz, Blues, Swing or Dixi.
Catering will be provided and as a special delicacy you may enjoy a suckling pig!
If you like to join the event please register at PHPUG-Munich Wiki and follow it for updates.
Alternatively you may register at Facebook as well and follow this for updates.
For any questions please visit IRC channel: #phprp on irc.uni-erlangen.de.
The PHP 5.3. BBQ release party is sponsored by:
Supporters for the PHP 5.3 BBQ release party are:
Jul 1: MySQLi result set iteration - recursive


PHP 5.3 is released and after the release stress is over my mind is open for new ideas. While relaxing yesterday I thought about many things, among them was the Resultset iterator I recently discussed.
Now I wondered where to go next with this and had the idea that an individual Resultset is a child of the whole result and this might be wrapped in an Recursive Iterator. For doing so we don't implement the Iterator interface but RecursiveIterator. RecursiveIterator extends a typical Iterator with two methods: hasChildren() and getChildren(). But now we have a problem: The Iterator returned by getChildren() has to be a RecursiveIterator, too, which makes sense, in general. But I want to return a MySQLi Resultset which isn't recursive - so making this a RecursiveIterator is wrong. My solution now is to introduce yet another Iterator which goes by the name of MySQLi_PseudoRecursiveResultIterator and is implemented by extending IteratorIterator which will wrap the MySQLi_Result and implements RecursiveIterator telling the caller that there are no children.
As a sidenote: In our experimental tree Andrey made MySQLi_Result an iterator but that's not yet in php.net's CVS (might need some more testing, and probably we might change the design there...) so I'm emulating this with MySQLi_Result::fetch_all() combined with an ArrayIterator, using the experimental code the constructor can be dropped.
So let's finally look at the code of these two classes:
<?php class MySQLi_ResultsetIterator implements RecursiveIterator { private $mysqli; private $counter = 0; private $current = null; private $rewinded = false; public function __construct(mysqli $mysqli) { $this->mysqli = $mysqli; } private function freeCurrent() { if ($this->current) { $this->current->free(); $this->current = null; } } public function rewind() { if ($this->rewinded) { throw new Exception("Already rewinded"); } $this->freeCurrent(); $this->counter = 0; $this->rewinded = true; } public function valid() { $this->current = $this->mysqli->store_result(); return (bool)$this->current; } public function next() { $this->freeCurrent(); $this->counter++; $this->mysqli->next_result(); } public function key() { return $this->counter; } public function current() { if (!$this->current) { throw new Exception("valid() not called"); } return $this->current; } public function hasChildren() { return true; } public function getChildren() { return new MySQLi_PseudoRecursiveResultIterator($this->current); } } class MySQLi_PseudoRecursiveResultIterator extends IteratorIterator implements RecursiveIterator { public function __construct(MySQLi_Result $result) { // This ctor can be dropped with the experimental bzr sources // as IteratorIterator::__construct() directly works with // MySQLi_Result parent::__construct(new ArrayIterator($result->fetch_all())); } public function hasChildren() { return false; } public function getChildren() { throw new Exception("This should never be called"); } } ?>
Now we can use this code. For properly using a RecursiveIterator one should use a RecursiveIteratorIterator (RII). To get some nice labels I'm extending the RII and then have a single foreach:
<?php class MyRecursive_IteratorIterator extends RecursiveIteratorIterator { public function __construct(MySQLi $mysqli, $flags = 0) { parent::__construct( new Mysqli_ResultSetIterator($mysqli), $flags | RecursiveIteratorIterator::LEAVES_ONLY); } public function beginChildren() { echo "Next ResultSet:\n"; } } $mysqli = new MySQLi("localhost", "root", "", "test"); $query = "SELECT 1,2 UNION SELECT 3, 4;". "SELECT 'hi world' UNION SELECT 'foobar'"; if ($mysqli->multi_query($query)) { foreach (new MyRecursive_IteratorIterator($mysqli) as $key => $row) { printf(" %s\n", $row[0]); } } ?>
Now calling this code gives us a result similar to the following:
Next ResultSet: 1 3 Next ResultSet: hi world foobar
Isn't that nice? - I think that's a cool API! What do you think? Do you have use cases for such an API? Should we implement this in C and bundle it with PHP? Any feedback welcome!