KBD

Keith Devens .com

Saturday, July 4, 2009 Flag waving
Men never do evil so completely and cheerfully as when they do it from religious conviction. – Blaise Pascal (attributed)
← Testing a Formation changeNewsForge | Linux needs better network file systems →

Daily link icon Sunday, December 12, 2004

Mixins in PHP (Or, I hate PHP volume 2)

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".

← Testing a Formation changeNewsForge | Linux needs better network file systems →

Comments XML gif

Harry Fuecks (http://www.phppatterns.com) wrote:

To an extent there's an alternative in Class kit: http://pecl.php.net/package/classkit - not quite the same but almost - of course you've got to compile it yourself. You might also get some mileage out of the _get(), set() and _call() "magic" methods

But agree - think object aggregation was a useful tool that should be restored. There's other buzz words it would make possible like AOP and Metaclasses.

The PHP 4.3.x implementation was good, IMO, as it encouraged use only for exceptional situations, rather than as an alterative to the current inheritance mechanism. So no ones going to go wild with multiple inheritance, which seems to be the "fear" that had it removed.

Some other people with thoughts in the same area;

http://jaxn.phpenterprise.net/blog/archi...Programming-is-Important-for-PHP.html
http://jaxn.phpenterprise.net/blog/archives/419-More-on-Aspect-Oriented-PHP.html
http://www.futuraworkz.com/index.php?p=8

∴ Harry Fuecks | 13-Dec-2004 3:01am est | http://www.phppatterns.com | #6610

Keith Gaughan (http://talideon.com/) wrote:

OTOH, classes just suck anyway. Prototypes Abú!

∴ Keith Gaughan | 13-Dec-2004 4:03pm est | http://talideon.com/ | #6615

83.30.198.249 wrote:

Looks like I am not alone in thinking that the newer php version we have the more limits we have: http://blog.gosu.pl/comments.php?id_news=7

∴ 83.30.198.249 | 14-Dec-2004 8:16am est | #6620

Joe Cheng (http://www.joecheng.com) wrote:

Can't you use PHP 5 delegates to fake mixins?

http://www.zend.com/zend/php5/php5-delegation.php

It all comes down to message passing, after all...

∴ Joe Cheng | 15-Dec-2004 6:04pm est | http://www.joecheng.com | #6637

Keith (http://keithdevens.com/) wrote:

Thanks for the link, Joe.

This doesn't do exactly what I want, but it's close. Unfortunately, it doesn't work under both PHP 4 and 5, so I have to program it the brain-dead way anyway if I want it to work under both. Though maybe I could do something to use object aggregation under PHP 4 and delegation under PHP 5.

However, it still enforces an artificial separation between the two since I can't do things like access the first class' instance variables from the delegated class through $this, but that's not such a big deal. Though, this is all probably much less efficient than the PHP 4 object aggregation feature since it involves temporary objects and lots of indirection.

Oh yeah, and I'm a huge fan of prototype-based object systems.

Keith | 19-Dec-2004 2:41pm est | http://keithdevens.com/ | #6667

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2009
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 3 posts

Recent comments XML

new⇒Wizard's First Rule

> while it is cheesy to some​extent, I actually found it to be​pretty enjoy...

Keith: Jul 3, 6:33pm

I hate Norton Antivirus

I bought Norton 2009 and it is not​installing on my computer!!!
It​seems l...

o'neil: Jun 30, 11:44am

Generated in about 0.233s.

(Used 8 db queries)