Re: [RFC] Propety Accessors v1.1

From: Date: Thu, 11 Oct 2012 01:56:23 +0000
Subject: Re: [RFC] Propety Accessors v1.1
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-63333@lists.php.net to get a copy of this message
On 10/10/12 21:41, Jazzer Dane wrote:
Here's my feedback on some current outstanding issues/suggestions: 1) Default value: I think having functionality for a default value is necessary, but I'm also thinking it may already be implementable within the current syntax.
class Test {
     private $seconds;
     public $hours {
         get() {
             if(!is_null($this->seconds)) {
                 return $this->seconds;
             } else {
                 return 10; // 10 is default
             }
         }
     }
}
The above should work fine in many scenarios, right? We could perhaps then claim that the issue may rather be that we need access to the variable *$hours* itself inside of the get/set/etc functions, which I think has been brought up before - though I'm not so sure how sensible that is. Whether we need that or not is up in the air.
I think the syntax for defaults would be: class UsingInitialValue {
    private $seconds = 36000;
    public $hours {
        get() { return $this->seconds/3600;}
    }
} class UsingLazyLoad {
    public $seconds {
        get() { return $this->seconds = 36000;}
        set($value) {$this->seconds = (int) $value;}
    }
} class UsingConstructor {
    public $seconds {
        set($value) {$this->seconds = (int) $value;}
    }
    public function __construct(){
        $this->seconds = 36000;
    }
} I think I've got those all right... Cheers, David

« previous php.internals (#63333) next »