For my Formation library what I really, really wanted was some way to do mixins in PHP. PHP doesn't support mixins or multiple inheritance natively even in PHP 5. But, I came across this post by whytheluckystiff over at Advogato from over two years ago where he tries to fake it (it turns out I blogged that link back then but had forgotten about it).
Through the comments on that post I found out about PHP's object aggregation functions. It turns out that does exactly what I need -- it simply stuffs all the methods in a given class into an existing object. Hence, a runtime, instance mixin. Literally, exactly what I needed. The only thing I would have liked is if the constructor for the mixin class could have been called upon mixing, but I don't think even Ruby does that for mixins.
However, when I checked out the documentation for the feature it had a big EXPERIMENTAL warning on it. I wouldn't want to use the feature in a library if it's not stable. So, I went looking around to see who to ask, and started poking around the dev mailing list. I searched for "aggregate" and found some references to bugs. So, I searched the bug db to see what it said there (incidentally, why don't they have the year of the bug report or its annotations listed anywhere??), and it turns out object aggregation isn't supported in PHP 5.
Have I mentioned I hate PHP? They added, but then took away exactly the feature I wanted. Now, because there's no multiple inheritance (even in PHP 5) -- which wouldn't do exactly what I want anyway -- I have to go back to manually creating a wrapper class that manually wraps all the methods in the wrapped class, and then you have to create an instance of that object separately and pass it an instance of the first class, etc. Is there any alternative?
Yet again, the PHP developers deprecate (with warnings!) or completely remove a feature without providing an acceptible alternative. How frustrating.
Addendum: To show how aggregate worked, here's some test code I whipped up:
<?php
class Foo{
function testFoo(){
echo "testFoo";
}
}
class Bar{
function testBar(){
echo "testBar";
}
}
$f = &new Foo();
aggregate($f, 'Bar');
$f->testFoo();
$f->testBar();
?>
Which prints "testFootestBar".
I hate ASP.NET
I hate ASP... I was doing wonderswith PHP, then suddenly one of myclients...
Johnies: Mar 17, 6:14am