Hi Everyone,
I've started a new RFC about PHP and mixins:
Stefan has done some really interesting research with traits though I'm
concerned about how intuitive it is.
Good summary of how mixins and traits deal with conflict here:
http://marc.info/?l=php-internals
http://marc.info/?l=php-internals&m=120337784031746&w=2
&m=120337784031746&w=2
Note: the rfc proposes to not to override any already defined methods.
Jonathan Bond-Caron wrote:
It seems to me that class_mixin() is a bad idea for the core; it's more
of a runkit type functionality than traits. After all, we don't allow
dynamic inheritance, or dynamic interfaces...
Personally I think that traits is a lot safer than mixing classes.There will
be a lot of things that will need to be considered in order to make it
logical for developers to use.
Quote from the RFC:
"Any method or property that already exists in the class or parent/extended
class cannot be changed unless using class_mixin() see below."
I'm pretty sure this is against all beliefs in the php world.
Runtime merging of classes and overriding methods is a no-no for me.
Traits seems much more safe since you cannot initialize a trait and can only
used to "flatter" other classes.
The proposed way of mixins will create confusion when trying to continue
development on an ongoing project and will need A LOT of extra documentation
just for explaining the hierarchy of the mixed classes.
A very simple case of mixins for php (that allready is implemented) can be
seen in Advogato: Mixins for php.
It has the following example.
class Person {
var $job = "person";
function show_job() {
echo "Hi, I work as a {$this->job}.";
}
}
class Bartender {
var $job = "bartender";
function show_job() {
echo "BARTENDER: ";
Person::show_job();
}
}
$b = new Bartender;
$b->show_job();
If you look at the example you can see that the Bartender class does NOT
extend the Person class, but when using Person::show_job(); the person's
method have access to the $b object's variables as if the bartender class
extended the person class.
IMO this is the safest way we can use mixins without getting into the
troubles of creating too many (complicated) ways to do the same thing.
G.
On Thu, Sep 11, 2008 at 7:07 AM, Edward Z. Yang <
edwardzyang@thewritingpot.com> wrote:
Jonathan Bond-Caron wrote:
It seems to me that class_mixin() is a bad idea for the core; it's more
of a runkit type functionality than traits. After all, we don't allow
dynamic inheritance, or dynamic interfaces...