On 12/06/16 14:03, Rasmus Schultz wrote:
> If I had to implement type-checked properties in plain PHP, I would do
> something like this...
> 
> /**
>  * @property int $price
>  */
> class Product
> {
>     private $_price;
> 
>     public function __set($name, $value) {
>         if ($name === "price") {
>             if (!is_int($value)) {
>                 throw new UnexpectedValueException("...");
>             }
>             $this->_price = $price;
$this->_price = $value;
>             return;
>         }
>         throw new RuntimeException("undefined property {$name}");
>     }
> 
>     public function __get($name) {
>         return $this->{"_{$name}"};
>     }
> }

I'm with you on not totally understanding the internals, but ...

var $_price;
Added code to handle the access restrictions, so checks have to be made
on just how $this->_price is accessed.
In my book, the __set function validates range as well as checking so
value is of the right type and any range error can be handled at the
right point. However if you add 'int' to the private then this now adds
a check to see if there IS a type restriction on the 'var' in addition
to access restriction.
Checks for 'strict' come in here as well?
But I still see a lot more cases where the 'is_int' checks are needed in
the normal flow prior to any ACTUAL assignment to the typed property. SO
it makes a lot more sense to me that '$this->_price = $value;' either
does the job of validation fully, or does not do it at all?
And there should be no distinction between $this->_price = $value; and
$this->params_array[_price] = $value; It is exactly the same variable?

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to