'Simple' phpers idea about interfaces/code reuse [was] Re: [PHP-DEV] PHP 6.0 Wishlist
| From: | Jochem Maas | Date: | Thu, 25 Aug 2005 09:44:39 +0000 |
| Subject: | 'Simple' phpers idea about interfaces/code reuse [was] Re: [PHP-DEV] PHP 6.0 Wishlist | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-18404@lists.php.net to get a copy of this message | ||
Andi Gutmans wrote:
Well $proxy would automagically support the interfaces of the aggregated objects. And if there's a method you want to proxy to one rather the other you could fine tune that. Anything more than that would be getting into an OOP pissing contest which I don't feel like getting into, as it would be academic and wouldn't be suitable for the PHP audience...in a totally non-academic and impure way I have always thought that it would be nice to be able to define function bodies in interface declarations ... I realise that every computer science major is now screaming 'no' but from a practical phper's POV it seems efficient to be able to say: interface Printable { function print() { // do simple generic stuff here // maybe go wild with some reflection :-) } } I would see this as a optional addition to the syntax of interfaces... i.e. everything works as before but you can add default function bodies which would behave as if it was defined in each class that implements the given interface. IMHO interfaces would be a stronger tool because of the increased code reuse, I find interfaces a very nice addition but I'm sometimes miffed at the need to redefine an identical function X times (in situations where there are already class hierarchies that do not lend themselves to incorporating the 'interface' implementation in some base class). if this is a truely evil concept I would very much appreciate a little heads up as to why :-) rgds, Jochem ps. and again (hey I'm a broken record too!) thanks for php! Pretty Happy Punter.
Andi At 03:43 PM 8/24/2005, Marcus Boerger wrote:Hello Andi, hu? Here's you example again: <?php $proxy = new Proxy() $proxy->aggregate($obj2); /* Also aggregates interfaces */ $proxy->aggregate($obj3); $proxy->delegate($obj3, "method_that_exists_in_both_objects"); ?> ok lets change this to an interface like in my example: <?php interface Printable { function print(); } class Document extends Proxy implements Printable { // now you need to implement the functions of Printable, only how? // implement them as empty and later overload them with their real body? } ?> So what do you want to do about the fact that my delegates are done at compile time while your aggregation is run at compile time? And no, at the moment there is absolutley no way than to completley rewrite the object/interface stuff in php to allow dynamic interfaces which your proposal would need become compatible. And even if you did you'd simply overthrow the idea of interfaces, wouldn't you? best regards marcus Thursday, August 25, 2005, 12:35:11 AM, you wrote:Not if it's done they way I think it can be done...At 03:17 PM 8/24/2005, Marcus Boerger wrote:simply isHello Andi, you mean fine tune the 'protocol' to use the correct wording. ItI don't mind not adding it, as long as we don't add "delegate" :) Anyway, it's different from MI because it allows you to fine tune the interface... Andiincompatible with the interface concept. Wednesday, August 24, 2005, 11:48:33 PM, you wrote:At 12:01 PM 8/24/2005, Marcus Boerger wrote:proxy/aggregation ideaHello Andi, i said 'we could probably add'. But even thoughexists ilike you offered and which i of course could implement in split onlystill see no reason for it. The problem with the latter is thatwithgives you back the original MI problems and doesn't work togetherwordsinterfaces at all. Thus i see it as contraproductive. Or in otherUnless itlet's simply not add something here and live with code reuse.that time weturns out in the future that it becomes the normal thing - byshouldAgain (I sound like a broken record), I don't think that wemight have PHP 8 wishlist ;-) regards marcus Wednesday, August 24, 2005, 1:27:20 AM, you wrote:add language constructs to support delegation. We will be overcomplicating things for PHP developers and it'll start tobe hardto read PHP code. I do think it might be worth considering a user-land or internal class such as a Proxy class which would allow the advanceduser tohave more find-grained control on delegation. I'd still keep it simple. So basically I'd allow aggregatingobjects(or delegatees), and define their priority in the order they are added. Then there could be one method which overrides thispriorityby allowing to map methods to objects. For example somethinglike:then$proxy = new Proxy() $proxy->aggregate($obj2); /* Also aggregates interfaces */ $proxy->aggregate($obj3); $proxy->delegate($obj3, "method_that_exists_in_both_objects");This would only be used in very advanced cases, most probably in frameworks but would be invisible to the average PHP developer. Complicating it more than this including the addition of language constructs doesn't seem good to me. If this isn't enough (and I"m sure there are some hypothetical cases where it might not be),you could achieve the same goal in other ways...PHP 6 or 7Still I am torn wether even this should be added. Andi At 12:57 PM 8/23/2005, Marcus Boerger wrote:At 19:12 23/08/2005, Lukas Smith wrote:Hello Zeev, Tuesday, August 23, 2005, 8:59:49 PM, you wrote:future version.Joseph Crawford wrote:I would like to see Multiple Inheritance implemented in aimplementingI am not sure what obstacles got in the way (if any) for notthat in PHP 5 but i would defenately like to see that inYou're right, it was, that's why we have interfaces.similar effects.IIRC, it was shot down. Use overloading and interfaces to getimpl for thethis is theExactly. And you can emulate MI with __call(). The only thing we could probably add is delegates to overcome the anti-code-reuse interface aspect. // Some interface interface Printable { function print(); } // A general implementation for the interface class PrintHandler implements Printable { function print() { /* ... */ } } // A standard user class that implements the interface. Typically// reason for MI since often you would just go with a standardOn the otheris an error// interfaced code. Actually common with containers (*). class Document implements Printable { delegate Printable $print; function __construct() {$this->print = new PrintHandler($this);} // No need to provide function print here - actually doing sothe delegate:// since tit's body is automatically generated as a call tobe NULL");// function print() { // if (!$this->print) throw new Exception("Delegate must not// $this->print->print(); // } } On the one hand this raises the complexity and syntax magic.otherwise a painway it is really elegant and promotes code-reuse which isit againa andbase classwith interfaces. (*) Actually the most prominent case is a general Container and acontainers too. Soin a GUI Framwork. There the desktop and the windows needyou'd habe an interface to access the container and implementmistake - nobodyagain...even though you already have it. And if you find atells you whether you fixed all incarnations of the code.