Re: __toString(), __toArray()
| From: | Chris Stockton | Date: | Tue, 12 Jan 2010 22:40:58 +0000 |
| Subject: | Re: __toString(), __toArray() | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46659@lists.php.net to get a copy of this message | ||
Hello,
On Mon, Jan 11, 2010 at 8:32 PM, mm w <0xcafefeed@gmail.com> wrote:
> cast is not needed in PHP
>
> i 'd rather be more interesting in
>
> class Obj {
> function __catch($data, $type) {
> //$type [ static_method, method, get_property, set_property]
> if (observed && $type == set_property &&
> somevalueIsObserved) {
> $observer->notify("somevalue::valueWillChanged");
> $this->somevalue = $data['somevalue'];
> $observer->notify("somevalue::valueDidChanged");
> } else {
> continue__call();
> }
> }
> }
What? ...
>> Etienne Kneuss wrote:
>> This is where operator over-loading would be useful however perhaps only
>> explicit casts would make sense here.
I beleive adding a __cast(string $type) would be a useful feature for
me, very often I have a toArray method defined. I agree with you that
due to unexpected edge cases with operator precedence and general type
juggling that __cast should only be called on explicit (cast).
I.E.:
class Int { public function __cast($type) { return 'int' == $type ? 15 : 0; } }
$r = new Int;
var_dump($r + 1); // 2
var_dump((int) $r + 1); // 16
var_dump((bool) $r + 1); // 1