Re: RFC: Traits for PHP
| From: | Larry Garfield | Date: | Tue, 19 Feb 2008 01:36:17 +0000 |
| Subject: | Re: RFC: Traits for PHP | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-35572@lists.php.net to get a copy of this message | ||
On Monday 18 February 2008, Richard Lynch wrote:
> On Mon, February 18, 2008 1:27 pm, php@stefan-marr.de wrote:
> > <?php
> > trait ezcReflectionReturnInfo {
> > function getReturnType() { /*1*/ }
> > function getReturnDescription() { /*2*/ }
> > }
> >
> > class ezcReflectionMethod extends ReflectionMethod {
> > use ezcReflectionReturnInfo;
>
> So it's just like an include for a re-used body of 'class' code.
>
> Hmmmm.
>
> Why not just allow 'include' here instead?
>
> :-)
Because include requires the code in question to live in another file, which I
don't always want.
It sounds interesting to me, and I can definitely see the value. I think I'd
suggest having multiple included traits override each other, however, so
that:
trait A {
function foo() { echo "A"; }
}
trait B {
function foo() { echo "B"; }
}
class C {
use A;
use B;
}
$c = new C();
$c->foo(); // prints "B" since that came second.
That said, the conventional OOP mechanism to get the same result would, I
think, look something like this:
interface iface {
function foo();
function bar();
}
class I implements iface {
function foo() { ... }
function bar() { ... }
}
class A implements iface {
protected $iface;
function __construct() {
$this->iface = new I();
}
function foo() { return $this->iface->foo(); }
function bar() { return $this->iface->bar(); }
}
The class/interface method takes a little more typing and an extra function
call on the stack, but otherwise what is the advantage of Traits over this
method? (Really, I'm genuinely curious.)
You also note that this mechanism has no runtime impact. That's unfortunate,
because I'd find the ability to add methods to an object at runtime
conditionally based on some other value far more useful in my work. :-)
Especially since, as above, there seems to be a way to implement this
functionality now as-is with a little more typing, yet runtime modification
is still impossible without eval() and similar scary stuff.
Vis, I'd find the following more useful in the code I write:
trait Baby {
function crawl() { ... }
}
trait Teen {
function complain() { ... }
}
class Person {
protected $age;
function __construct($age) {
$this->age = $age;
if ($this->age <= 3) {
$this->addTrait('Baby');
}
if ($this->age >=13 && $this->age <=19) {
$this->addTrait(Teen');
}
}
}
$p[1] = new Person(19);
$p[1]->complain();
$p[2] = new Person(1);
$p[2]->crawl();
foreach ($p as $person) {
if ($p instanceof Teen) {
$person->complain();
$person->parent->ground($person);
}
}
I don't know if that's technically not feasible or technically not a Trait
anymore and therefore off topic in this thread (if it is, that's fine, let me
know and I'll shut up about it <g>), but that strikes me as more useful than
just runtime composition.
--
Larry Garfield AIM: LOLG42
larry@garfieldtech.com ICQ: 6817012
"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson