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 pecl4win.php.net 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.