Etienne Kneuss

home » news

SPL Datastructures

The Standard PHP Library was recently completed by a couple of data structures, namely heaps and doubly linked lists. You can find more information about those on
php.net/spl.datastructures

Feeds available

You can read colder.ch directly from the rss/atom feeds:
rss general RSS (general)
rss php RSS (PHP)
atom general Atom (general)
atom php Atom (PHP)

New design : Clearblue

I've made a new skin using the same design with blue colors: Clearblue. Check it out

New website

As you can see, the website has changed completely. I've re-designed everything using different types of technology to give an overall improvement. Take a look in the news section for detailed information.

The 13th of January 2010

Dataflow Type Analysis for PHP@ 16:49

0 comment

I've spent some time lately working on a project involving data-flow analysis for PHP.

This analyzer will basically model your code as control flow graphs, in which it will assign types and let them flow through control structures. When reaching stability, it will check that the operations done of the values are sound type-wise. It will also do some structural checks.

For more details, I suggest you read the technical report that you can find here:
http://project.colder.ch/report.pdf

You can also find a presentation I gave recently about it: http://github.com/colder/phpanalysis/raw/master/presentation/presentation12.01.10.pdf

This project is in an early stage, a prototype, but still gives some results!

If you try it out, no need to report and tell me "Hey this fails and this and this", I know. It's not supposed to work for every input code yet! However, I would happily accept any contribution!

You will find the github project page here: http://github.com/colder/phpanalysis

ps: I just missed the 1 year "no-news anniversary", damn!

The 8th of January 2009

SplObjectStorage for a fast and secure object dictionary@ 03:03

8 comments

There have been activity recently about the different ways to uniquely identify objects in PHP. In fact, a function have been sitting in SPL unnoticed for quite some time and while people came across it, some got frustrated. I'm of course talking about spl_object_hash(). To summarize it: in PHP, you basically need two things to safely identify an object: a object index, the handle, and the class handlers which is how the object will react internally. This set of handlers is actually a pointer, and since disclosing valid pointers is not something that should be done, spl_object_hash is simply providing a MD5 hash of those two values concatenated. Now two problems comes from this MD5 hash:

  • It's quite slow
  • It may generate collisions

One of the usages of this hash that comes to mind is an object dictionary(or map): attach information to instances, for example:

<?php $dict = array(); $dict[spl_object_hash($obj1)] = $info1; $dict[spl_object_hash($obj2)] = $info2; // and so on. ?>

Sadly, since PHP arrays are themselves hashtables, that means the hash will get hashed one more time, this is a waste of time.

Another example could be to mark nodes in a graph traversal algorithm, using a set of visited nodes.

SPL thankfully provides a class (as of PHP5.3) that can implement quite easily both examples without the problems stated above: SplObjectStorage.
Since an example is better than thousand words, here is a demonstration:

<?php // Map $dict = new SplObjectStorage; $dict[$obj1] = $info1; $dict[$obj2] = $info2; var_dump($dict[$obj1]); // $info1 // Set $set = new SplObjectStorage; $set->attach($obj1); var_dump($set->contains($obj1)); // True ?>

SplObjectStorage directly uses the unique identifier without pre-hashing it, so you spare time and you will be safe against collisions!

The 7th of June 2008

SplFastArray to speed up your PHP arrays@ 04:01

20 comments

Antony got the idea to implement a C-like array wrapper in SPL: SplFastArray. The main advantage of that class is performance, it's indeed faster than PHP arrays. How so? No free lunch: The speedup comes from the fact that non-numeric indexes are not allowed and that the array is of fixed size (which means no hashing and continuous memory storage). That said, here is a quick example:

<?php $a = new SplFastArray(4); // takes the initial size as first argument $a[0] = "foo"; $a[2] = "gee"; $a[3] = "plop"; $a->setSize(3); $a->setSize(5); // increase the size foreach($a as $k=>$v) {     var_dump($v); } /* Output:  * string(3) "foo"  * NULL  * string(3) "gee"  * NULL  * NULL  */ ?>

The speedup seems to vary between different environments, but so far the multiple benchmarks showed that SplFastArray was 10~30% faster than standard PHP arrays.

The 12th of May 2008

SPL Datastructures updated@ 18:34

2 comments

Here is some news about data structures available in SPL:

  • There finally is documentation for SplDoublyLinkedList, SplStack and SplQueue.
  • "New" classes: SplHeap (abstract), SplMaxHeap, SplMinHeap and SplPriorityQueue, documentation of those classes is in progress.

Here is an example of a simple but task scheduler using SplPriorityQueue:

<?php $q = new SplPriorityQueue; $q->insert('a', 1); $q->insert('b', 4); // ... $q->insert('z', 2); foreach($q as $task) {     // .. process task ..     echo $task."\n";     // ... add new tasks ..     if ($task == 'b') {         $q->insert('c', 3);     } } /* Output:  * b  * c  * z  * a  */ ?>

This implementation is really efficient as the underlying data structure of SplPrioritiyQueue is a Heap, which allows insertions and extractions in O(log2(N)).

The 16th of January 2008

New datastructures in SPL@ 23:18

5 comments

New datastructures made their way into SPL recently:

  • SplDoublyLinkedList

as long with two childs:

  • SplStack
  • SplQueue

Those structures implement Iterator, ArrayAccess and Countable which makes them easy to handle. There are also multiple ways to iterate through them.

More datastructures will get implemented in the future. The goal is to implement quite every basic datastructure along with algorithms related to them. SPL could then be used as an object oriented replacement for ADT.

The 24th of August 2007

Late Static Bindings explained@ 17:38

1 comment

One of the features that are expected for PHP6 is late static bindings. While discussing the matter over IRC, I noticed that this subject was not clear for everyone. I hence wrote a little article describing in examples its usage:
Late Static Bindings explained

The 30th of April 2007

For docbook+vim users@ 20:00

1 comment

Being repeatedly asked to write a set of vim syntax rules or mappings to help doc writers, I now have a file that contains a decent amount of useful stuff.

As this may be helpful to others out there, I decided to make it public.

Here is a list of the features that this script provides:

Syntax improvments:

  1. Highlight PHP code in examples
  2. Define folding rules for <refsect1>

Mappings:

  1. @fd : Function definition section
  2. @fpm : Function parameter normal
  3. @frpm : Function parameter by reference
  4. @fopm : Function optional parameter
  5. @forpm : Function optional parameter by reference
  6. @pms : Parameters section
  7. @vle : Variable list entry
  8. @ret : Return values section
  9. @err : Errors section
  10. @uni : Unicode sections
  11. @chlog : Changelog section
  12. @ex : Single example
  13. @rex : Example section

Some of those bindings use placeholders (<++>). Usually, Ctrl+j is used to jump through them.

I will probably continue adding new stuff in that script when I feel the need to.

Here is the script.

References:
Doc Skeletons

The 27th of January 2007

The truth about mod_rewrite's Last modifier@ 22:25

0 comment

After arguing once again on IRC about mod_rewrite's [L] (Last) modifier, I decided to write an article about how it really works. It also explains how mod_rewrite internally works.

You can read it here: The truth about mod_rewrite's Last modifier

The 6th of December 2006

PHPT Generator used to discover documentation problems@ 17:52

3 comments

I commited a patch to docweb, some days back, to allow the generation of the complete package of phpt files. Those phpt files were generated from doc examples.

The main use of those phpt files was to have an easy way to check examples used in the documentation. Along with a php script analyzing the results and Hannes' great testing environment, I was able to generate a summary of the examples failing:

The results are quite interresting:

  1. 251 tests were generated
  2. 126 (50%) passed the checks
  3. 23 (9%) tests failed because of a missing extension
  4. 102 (41%) tests failed because of an error in the example (11% of them were caused by whitespace issues only)

There is thus potentially 80-90 examples in the documentation that contain errors!

Let the massive example bug squashing party begin !

The 18th of November 2006

Introduction to debugging for beginners@ 01:42

0 comment

Another "guide" :) Well, this one talks about practices of debugging in php userland.

The intended audience is clearly beginners. I hope it'll help some of them.

You can read it here:
Introduction to debugging

Trip to norway@ 00:47

0 comment

I made a trip to Norway last summer with a friend: pretty amazing land.

We took some pictures using cheap and broken cameras. However, some of them are decent, and I created a slideshow on flickr:

http://www.colder.ch/norway

The 4th of October 2006

.phpt files generator finally up!@ 03:22

0 comment

I'm happy to announce the availability of a phpt files generator on http://doc.php.net.

This script is meant to scan the php manual for examples, which can then be used to generate phpt files. Those phpt files will help to:

  1. spot mistakes in examples
  2. extend the code coverage analysis

After a lot of discussions on IRC and hours of work, this project was successfully completed. And I would like to thank Hannes Magnusson, Ligaya Turmelle, Philip Olson and Sean Coates for their support.

The 30th of August 2006

Spam issue in the comments@ 17:34

0 comment

I've recently encountered spam issues with my comments system. I've thus implemented a simple spam challenge that should be able to stop that spam.

However, if you posted a comment recently, it may has been removed in the lot, so feel free to post it again.

The 17th of June 2006

Quick guidelines to code cleanliness@ 15:26

0 comment

I wrote a quick guide containing some guidelines for code cleanliness for beginners.

You can read it here:
Quick guidelines to code cleanliness

This might be interresting for beginners willing to start with a good basis.

The 9th of September 2005

Woops@ 18:45

0 comment

I just noticed I had deleted the news about the implementation of "Another example showing that register_globals is evil !" :

Another example showing that register_globals is evil !

The 31st of August 2005

PHP's magic_quotes imcompetence against XSS@ 15:50

0 comment

I've wrote this publication about XSS and magic_quotes_gpc which shows that magic_quotes_gpc is not a solution against XSS:
Demonstration of PHP's magic_quotes imcompetence against XSS

The 19th of July 2005

New features@ 06:16

3 comments

I added the possibility to comment the news. This new feature will be followed by a "Old news" page.

I also noticed a little error in register_globals_is_evil.html which is corected right now. This publication will also be soon completely implemented in the website to allow comments, feedbacks, etc.

I wish you a nice visit, and hope I'll have more time to write some news in the near future.

The 23rd of May 2005

BTefnet is definitively down@ 20:36

0 comment

www.btefnet.net was one of the famous websites providing torrents of TV shows. The website shutdown some weeks ago and the future of btefnet was uncertain. Sadly, the topic of the official IRC channel clears the minds : "#BT will no longer be releasing any torrents or have a website".

The 10th of May 2005

New website available@ 22:16

0 comment

Here we go ! I'm happy to present you my new website. You probably feel a bit disappointed and I can understand that: the new design is completely different. I'll probably take the time to create a skin looking like the old website.

This new website is still under construction but the main work is done. I've made this new design thinking about two problem that occurred in the last one : shortage of place and the excessive weight of the images. As you can see, this design uses few images and its width is not fixed.

About the main system used behind: I changed the way I linked the pages, I used output buffering to have more control on the output but also to have control on the meta information that became dynamic. I also use a skin system using templates and CSS. Notice that I plan a "My settings" section where you'll be able to configure your own view of this website.

A repository system is also implemented to handle my php scripts, publications, school files and images. I'll fill it when I find the time.

I hope you'll enjoy.