Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62148 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27869 invoked from network); 14 Aug 2012 16:49:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2012 16:49:54 -0000 Authentication-Results: pb1.pair.com header.from=ralph@ralphschindler.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=ralph@ralphschindler.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain ralphschindler.com from 209.85.216.170 cause and error) X-PHP-List-Original-Sender: ralph@ralphschindler.com X-Host-Fingerprint: 209.85.216.170 mail-qc0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:54977] helo=mail-qc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 40/8E-00812-1B18A205 for ; Tue, 14 Aug 2012 12:49:54 -0400 Received: by qcmt36 with SMTP id t36so514055qcm.29 for ; Tue, 14 Aug 2012 09:49:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=5pMv+BZVczzcHQ0jDbXNCNN9jOq7TN2m1iAqYkEdP8w=; b=JLA61eQ9dEUGbdM+N4awFYgCMch2zjEv+MqrRXMOwWt+opUH0ZzlTUShNZJ7KYymmG 44gBV8ZI4s5EkHiChNTM1lhHqQW/OIc2r9MMn1qyFN3iV67RgeDLv+z5+Jc9Lra9NQt/ j0GcXgj1q+Ur5+TOTuKSczdjdnYSFpZFgmpej44xCzodsyKB5mi61sMIs2urfSjYTPsK Nkj6M/bJxUBG648r1TUmB0+8uTw12NhkaX4B2+CmB7dih7pwjXjlCaNltt+jz+3zd6dz OgPe9aW3fwYpyObjkHXlOiNcNrcxp2URwHK9bA7nIj6ua5p4MVT8n6uONJUcLaFkMo0S li3Q== Received: by 10.60.3.194 with SMTP id e2mr7758053oee.1.1344962990246; Tue, 14 Aug 2012 09:49:50 -0700 (PDT) Received: from Ralphs-Mac-Pro.local (ip174-73-14-247.no.no.cox.net. [174.73.14.247]) by mx.google.com with ESMTPS id o9sm2497415oeg.5.2012.08.14.09.49.49 (version=SSLv3 cipher=OTHER); Tue, 14 Aug 2012 09:49:49 -0700 (PDT) Message-ID: <502A81AD.1070303@ralphschindler.com> Date: Tue, 14 Aug 2012 11:49:49 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQlCfaAfU0A4uAbsc6cabMcWO+PsRGDhH4vmlmVJGowt9O4CziAhlVosB4xAl2ZfMsht/N+J Subject: Re: [PHP-DEV] Decorators Revisited From: ralph@ralphschindler.com (Ralph Schindler) In general, I think it would be nice to have something that does this for you, but I am not necessarily a fan of changing the meaning of instanceof. > That's a lot of boilerplate for each possible iteration. This is one reason > people like traits so much, as it's easier to just do automated copy/paste > than use the proper patterns. Couldn't a dynamic trait be a better? It would work like this: trait SplDynamicProxyTrait { protected $proxyObject; // enumerate all public method with the following: public function $name($signature) { return $this->proxyObject::$name($signature); } } usage would then be the following: class MyDecorator implements FooInterface { use SplDynamicProxyTrait; // compile time "code generation" public function __construct(Foo $foo) { $this->proxyObject = $foo; } // override trait public function method2($a) { if (!$this->hasCache('method2', $a)) { $ret = $this->proxyObject->method2($a); $this->setCache('method2', $a, $ret); } return $this->getCache('method2', $a); } } > So, example code like: > > class Foo {} > class Bar extends SplDecorator {} > $b = new Bar(new Foo); > var_dump($b instanceof Foo); // true > > It also works with type hints: > function test(Foo $f) {} > test($b); These would all still work, instanceof would actually be an instanceof though. > Now, there's a lot more to do (property cascading, interface validation, > etc), but the initial proof-of-concept is there. > What do you think? Is this a route that I should continue down? Or is there > something fundamental that I'm missing here? I know that Reflection, > get_interfaces(), etc would need to be updated to account for this. > > Thoughts? In the above scenario, we've reused traits and we haven't broken instanceof or the liskov principle (which basically your implementation does since SplDectorator bypasses all method signature checking until runtime). Thoughts? :) -ralph