Archive for February, 2007

Yahoo! Pipes

The web is buzzing about Yahoo! Pipes. It’ll be interesting to see what people build with Pipes. I couldn’t think of any need I had that would be fulfilled by Pipes, until today. I host this site on TextDrive / Joyent, and they have a forum that I lurk on from time to time. I’m mostly just interested in what a few select people say, particularly the top few people who work at TextDrive. Unfortunately on that forum, there aren’t any RSS feeds available that will monitor particular users’ posts. Until now, I’ve been using a bookmark to do a search on the forum that returns the latest posts from a specific user. In about 10 minutes of playing with Yahoo! Pipes, I had a working Pipe just to my liking. Hopefully, this does the trick for me.

I’m curious to see if I’ll “miss” posts by the select three users. The feed I’m filtering, contains the 15 most recent posts to the forum. This means that each post might not be listed in the feed for very long, as it may get pushed down by more recent posts. Presumably, either my RSS reader would have to access to Pipe or possibly Yahoo! Pipes would have to access the feed during that window in. If they don’t, then I’ll miss a post. It seems like this might be a problem for a significant number of uses for Yahoo! Pipes, so I’m interested to see how that plays out. Granted this “problem” is inherent in feeds, but it may be compounded when you’re filtering lots of feeds for specific information.

While we’re on the topic of working with feeds and publishing the results, it is worth noting you can do some simple manipulation and publishing with Google Reader, as well.

Comments

The static keyword in PHP5

With PHP5, PHP introduced some more robust object oriented capabilities to the language. I’ve been diving into this quite a bit lately and learning a lot. I figured all the stuff I discovered about the static keyword would be enough fodder for a post. Most of this information can probably be gleamed straight from the docs, but I for one didn’t really soak up the denseness of the documentation my first few times around.

Static Members

What isn’t immediately obvious, is that static members are shared among all the instances of the class that has the static member. In other words, it is like a global variable for that class and instances of that class. While you can’t access a static member directly from an instantiation of the class, you can create methods that access or manipulate the static member. Time for a quick example.

  1. class Foo {
  2.   static $var = 0;
  3.   function increment() { self::$var++; }
  4.   function get() { return self::$var; }
  5. }
  6.  
  7. $f1 = new Foo();
  8. $f1->get();  // returns 0
  9. Foo::$var; // returns 0
  10. $f1->increment();
  11. $f2 = new Foo();
  12. $f2->get();  // returns 1
  13. $f2->increment();
  14. $f1->get();  // returns 2
  15. Foo::$var; // returns 2

Now, the other interesting find is that the static member is global not only to that class and instances of that class, but also all sub-classes and instances of those sub-classes. You can prevent this propagation down inheritance tree, by declaring the static member as private or by redeclaring that static member inside the sub-classes, as well as the methods that work on the static method.

  1. class Foo {
  2.   static $var = 0;
  3.   function increment() { self::$var++; }
  4.   function get() { return self::$var; }
  5. }
  6. class Bar extends Foo { }
  7. Foo::$var++;
  8. Bar::$var;  // returns 1

Static Methods

The main area that I got hung up with static methods was using inherited static method calls. For the work I was doing, I wanted to be able to call a static method and within the static method know which class this method was called on. Unfortunately, everything I tried didn’t work.

  1. class Foo {
  2.   static function get_my_class() { return get_class(); }
  3.   function get_my_class2() { return get_class($this); }
  4. }
  5. class Bar extends Foo {}
  6. Bar::get_my_class();  // returns ‘Foo’
  7. $b = new Bar();
  8. $b->get_my_class2();  // returns ‘Bar’

It turns out that static methods belong to the class in which they’re defined and they have no clue where they got called from. Similarly, the same idea can be applied when thinking about static members. The class that has the actual code, determines what variable is affected. Consider one last example:

  1. class Foo {
  2.   static $var = 0;
  3.   function increment() { self::$var++; }
  4.   function get() { return self::$var; }
  5. }
  6. class Bar extends Foo {
  7.   static $var = 23;
  8.   function increment() { self::$var++; }
  9. }
  10. $f = new Foo();
  11. $b = new Bar();
  12. $b->get(); // returns 0 (Foo)
  13. $b->increment();  // increment Bar’s $var
  14. Bar::$var; // returns 24 (Bar)
  15. $b->get(); // returns 0

Comments (2)