Re: RFC: Traits for PHP

From: Date: Thu, 21 Feb 2008 18:32:08 +0000
Subject: Re: RFC: Traits for PHP
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-35698@lists.php.net to get a copy of this message
On 21.02.2008, at 19:09, Andi Gutmans wrote:
I don't think so. I think the value here is not copy&paste but to be able to encapsulate some functionality which a class can be decorated with. Giving a basic storage mechanism to that would be very beneficial in a large amount of uses. Again, I am proposing private storage only which would be for internal use-only by the decorating methods.
if someone desperately needs that they could just write a method with a static variable. class foo {
    function bar($value = null) {
        if (!isset($var)) {
            static $var = 'bar';
        }
        if (func_num_args() > 0) {
            $var = $value;
        }
        return $var;
    }
} $foo = new foo(); var_dump($foo->bar()); var_dump($foo->bar('foo')); var_dump($foo->bar()); ok its a hack .. but you can see how this could work. and you could even expand it to handle an arbitrary number of "properties" in a self contained method. all you need to do is find a name that doesnt clash. again i prefer to keep the feature as simple as possible without crippeling it the point of not being useful.
regards,
Lukas

« previous php.internals (#35698) next »