Hi Tom,
On 17 Apr 2009, at 19:20, Tom Boutell wrote:
How ever, there are also corner cases. (Behavior might be different
in case
of name clashes for dynamically created properties within a
composition,
changing behavior depending on the order of method invocations on a
single
object)
I'm not sure I grasp how these can happen in this scenario.
Well, it actually depends on the strictness of this idea. And its
particular implementation.
With the dynamic nature of PHP in mind, my first idea would be
something like this, which is not ideal:
class Base {
// private $a; <- intentionally left out because of
// some smart approach to use meta-programming
powers...
public function setA($value) {
$this->a = $value;
}
public function echoA() {
echo $this->a;
}
}
trait TraitUsingA {
// var $a; <- intentionally left out because of another smart
approach for using
// really clever meta-programming, not shown here
public function setTraitA($value) {
$this->a = $value;
}
public function echoTraitA() {
echo $this->a;
}
}
class MyClass {
use TraitUsingA;
}
$a = new MyClass();
$a->setTraitA('trait');
$a->setA('base');
$a->echoTraitA(); // echos 'trait'
$a->echoA(); // echos 'base'
$b = new MyClass();
$b->setA('base');
$b->setTraitA('trait');
$b->echoTraitA(); // echos 'trait'
$b->echoA(); // echos 'trait'
The question here is how to handle property accesses, in particular
accesses to unspecified properties.
I actually would expect to have a lookup mechanism which first looks
in the trait, and if the property is not found there, its going to the
object. I expect this behavior, because it is similar to what we have
with inheritance and properties defined by a superclass.
If a property is not jet defined, I would expect it to be created in
the most inner scope, like local variables.
On the other hand, I also would expect, as in inheritance, properties
to be found defined by super classes, or the class which defines the
composition.
But this only my intuitive solution, so this could of course be
specified different.
Best regards
Stefan
--
Stefan Marr
Programming Technology Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://prog.vub.ac.be/~smarr
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php