In Javascript, you could compose an object piecemeal. For example, you can start with:
var foo = {};
Then extend this empty object by assigning lambdas to its properties.
foo.bar = function () { alert('bar'); }
foo.bar(); // bar
You can even pull a method from another object and assign it to this property.
var goo = {
gar: function () { alert('gar'); }
};
foo.bar = goo.gar;
foo.bar(); // gar
Or a privileged method defined in a constructor:
function Xoo(x) {
this.xar = function () { alert(x + 'ar'); }
}
moo = new Xoo('m');
foo.bar = moo.xar;
foo.bar(); // mar
This is so easy and natural because Javascript makes no distinction between methods and properties. Javascript objects can be likened to an associative array in PHP. If the array member is callable, then it is a method which can be called via its key. Otherwise, it’s just a property.
PHP isn’t quite like that, but it is possible to simulate that behavior with a bit of magic. With the new lambda syntax, it becomes even closer to the Javascript way of extending objects.
read more…
This article (among others) states that it is possible to use the $this variable in a closure defined in a class method even without importing it via use(). I also read the RFC from the PHP wiki and it says the same thing. Why then does this code fail?
class foo
{
public $bat;
public function __construct($bat)
{
$this->bat = $bat;
}
public function bar()
{
return function() {
return $this->bat;
};
}
public function baz()
{
return $this->bar();
}
}
Using 5.3RC1/win32, it dies when bar() is called outside or inside (via baz()) the class, saying $this is used in a non-object context. It doesn’t allow ... function() use($this) { ... either, saying that $this cannot be used as a lexical variable. What’s happening?
To work around this, right now I’m assigning $this to $self above the closure definition and appending use($self). Seems to work as expected, except that I can’t access private members (which is probably intentional and exactly the thing I was exploring when I made this test). Why can’t I just import $this directly then? And whatever happened to the auto-import they promised?
Update: Man, I totally missed this. This page is linked in the closure RFC. They should have removed that entire section in the RFC instead, like they did to the lexical keyword (I can’t seem to find it anymore, or maybe I just imagined it). I just skimmed through it (since I’ve read it before) and missed the link entirely. So $this really cannot be imported at the moment, but it appears that they might be in the future.
I use the magic render* methods a lot nowadays, but I don’t use them exclusively. Elements with the same markup are better off decorated and rendered whole to minimize code duplication. Consistency and predictability of the markup is another advantage of decorators. Write it once, assign it to your elements and you can be sure that all your elements will be marked up the same way. No need to worry about mismatched tags (if you got them right in the first place) and you have fewer stuff to type.
I keep a static class which stores various sets of decorators so I can just pull them out of the class and assign them to my form elements where I see fit. There’s not a lot of them at the moment though.
read more…
The next minor release version of PHP went into beta a little over a month ago. Although it is a minor release in name, it includes a couple of major features that will no doubt change the face of PHP apps and libraries in the near future: closures and namespaces.
I myself have been waiting for the chance to try out these two new features. I tried the alpha earlier this year but I had major problems getting it to work in Windows and Apache 2.2. Turns out the beta isn’t that much better in that regard.
read more…
Decorators are a pain. To make a Zend_Form appear exactly as you want it to, you’ll have to fight with decorators and its cryptic, unintuitive syntax. There are many blog posts and tutorials extolling the flexibility and power of decorators, but they’re not being completely honest. They don’t mention how many hoops you’ll have to jump through to make it bend to your will.
read more…