Re: Traits and "horizontal composition of behavior"
| From: | Christoph M. Becker | Date: | Sat, 05 Nov 2016 15:19:34 +0000 |
| Subject: | Re: Traits and "horizontal composition of behavior" | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969386308@lists.php.net to get a copy of this message | ||
On 05.11.2016 at 15:57, Jeff M wrote:
> Hi, everyone. I have a question about traits and composition. On the traits
> docs page
> <http://php.net/manual/en/language.oop5.traits.php>, it's
> described as "horizontal composition of behavior". My question is, does the
> word "composition" here mean the same as when the GoF said to "favor
> composition over inheritance"?
>
> More specifically, if I have inheritance:
>
> class Foo { public function printItem($string) { echo 'Foo: ' . $string
> ... PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; }
> } class Bar extends Foo { public function printItem($string) { echo 'Bar: '
> ... $string . PHP_EOL; } }
>
> And I change it to traits:
>
> trait Foo { public function printItem($string) { echo 'Foo: ' . $string
> ... PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; }
> } class Bar { use Foo; public function printItem($string) { echo 'Bar: ' .
> $string . PHP_EOL; } }
>
> Does that mean I'm "favoring composition"?
This mailing is for discussing the PHP documentation. For support
questions write to the general user list (<php-general@lists.php.net>),
please.
--
Christoph M. Becker