Traits and "horizontal composition of behavior"

From: Date: Sat, 05 Nov 2016 14:57:23 +0000
Subject: Traits and "horizontal composition of behavior"
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969386306@lists.php.net to get a copy of this message
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"? Thanks.

« previous php.doc (#969386306) next »