pecl/operator and Other Neat Stuff

  • Posted by Mike Naberezny in PHP

    Sara Golemon‘s newest extension, pecl/operator, seems to have quietly slipped under the radar. This extension adds operator overloading support to PHP 5. I don’t think operator overloading fits the “PHP spirit” and as such I speculate it probably won’t ever make it into the core. Regardless, it’s interesting that this extension is now available and certainly makes for some fun experiments, especially if you’re already familiar with techniques from languages like C++.

    Here’s a very simple example for demonstration:

    class N {
      public function __add(N $x) {
          return new N();
    var_dump( $n1 = new N()   );  // object(N)#1
    var_dump( $n2 = new N()   );  // object(N)#2
    var_dump( $n3 = $n1 + $n2 );  // object(N)#3

    As you can see above, pecl/operator adds some new magic methods. There’s no documentation yet, however you can see the list of new methods by browsing the source for the extension. It’s relatively short and, for the most part, the new methods are intuitively obvious from the names.

    Remember the debates over a String class on php-internals? With pecl/operator, you can do some fancy things on your own in userland. There’s many possibilities of what you can do with this, so go have some fun. Users on UNIX-like operating systems can compile the extension as usual. Windows users will be pleased to find binaries over on Edin Kadribasic‘s excellent site.

    On a somewhat related topic, I’ve read a fair number of posts from PHP bloggers lately who are discovering languages like Ruby and commenting that metaprogramming (self-modifying code) isn’t “possible” in PHP. You can do some very powerful things with another extension from Sara: pecl/runkit. Even in vanilla PHP, you can also do some nice tricks by include()ing runtime-generated code through the stream functions. These, combined with PHP 5′s object overloading capabilities, make PHP a pretty rich environment for all sorts of hackery. It’s not that metaprogramming isn’t possible in PHP, it’s just non-obvious or inconvenient. Most of these ideas are not applicable to solving everyday problems but it’s always interesting to try your creativity and push the limits.

    Millions of programmers lead very productive and expressive lives without things “missing” in PHP like metaprogramming, lambda functions, and mixins. However, everyone is talking about them these days and understandably so — they’re neat. There’s a great deal of other neat stuff in different languages out there to inspire you also. Whether or not all of these things, including pecl/operator, are necessary or even desirable for everyday web development is a continuous debate. However, what’s not easily debatable is that learning a new language or even just reading about one can only serve to broaden your experience, give you new ideas and ways to approach problems, and make you a better PHP programmer for it.


  • comment by Stefano Forenza 1 Feb 06

    Would you post a code example of
    “some nice tricks by include()ing runtime-generated code through the stream functions.” ?


  • comment by Mike Naberezny 1 Feb 06

    You probably have a great example of this already: the PHAR archive format (PEAR’s PHP_Archive:

    Your PHP program can write any PHP source code you’d like into a string. This then gets include()d through a stream wrapper (see You can do anything you’d like to the string, including completely generating it at runtime.

    You can even go so far as to overwrite existing classes (see by rewriting the source code for an existing class into a string at runtime, and then feeding the string to runkit_import() through a stream wrapper.

  • comment by Radek Tetik 24 Nov 07

    By the “spirit of PHP” you mean PHP should be a simple language for simple tasks? Zend now moves PHP into the enterprise so advanced features should be added to language to be competetive. Operator overloding is a nice feature for string, date and similiar classes. It makes life easier.

Post a comment

Thanks for commenting. Please remember to be nice to others and keep your comment relevant to the discussion. I reserve the right to remove comments that don't meet these simple guidelines.