Hi,

> -----Original Message-----
> From: Aaron Piotrowski [mailto:aa...@icicle.io]
> Sent: Wednesday, July 1, 2015 9:00 PM
> To: Sara Golemon
> Cc: pthre...@pthreads.org; internals@lists.php.net
> Subject: Re: [PHP-DEV] [RFC] UString
> 
> 
> > On Jul 1, 2015, at 1:06 PM, Sara Golemon <poll...@php.net> wrote:
> >
> > On Tue, Jun 30, 2015 at 10:36 PM, Joe Watkins <pthre...@pthreads.org>
> wrote:
> >> Another possible issue is engine integration:
> >>
> >>    $string = (UString) $someString;
> >>    $string = (UString) "someString";
> >>
> > That sounds as a cool idea to discuss as a completely separate,
> > unrelated RFC, and not specific to UString.
> >
> > e.g.   $obj = (ClassName)$arg;   /* turns into */ $obj = new
ClassName($arg);
> >
> > So you could use casting with any class which supports single-argument
> > constructors.
> >
> > But again, orthogonal to this RFC.
> >
> > -Sara
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List To unsubscribe,
> > visit: http://www.php.net/unsub.php
> >
> 
> Expanding on this idea, a separate RFC could propose a magic
__cast($value)
> static method that would be called for code like below:
> 
> $obj = (ClassName) $scalarOrObject; // Invokes
> ClassName::__cast($scalarOrObject);
> 
> This would allow UString to implement casting a string to a UString and
allow
> users to implement such behavior with their own classes.
> 
> However, I would not implement such casting syntax for UString only. Being
able
> to write $ustring = (UString) $string; without the ability to do so for
other classes
> would be unusual and confusing in my opinion. If an RFC adding such
behavior
> was implemented, UString could be updated to support casting.
> 
> Obviously a UString should be able to be cast to a scalar string using
(string)
> $ustring. If performance is a concern, UString::__toString() should cache
the
> result so multiple casts to the same object are quick.
> 
One way doing this is already there thanks
https://wiki.php.net/rfc/operator_overloading_gmp . Consider

$n = gmp_init(42); var_dump($n, (int)$n);

However the other way round - could be done on case by case basis, IMHO.
Where it could make sense for class vs scalar, casting class to class is a
quite unpredictable thing.

While users could implement it, how is it handled with arbitrary objects?
How would it map properties, would those classes need to implement the same
interface, et cetera? We're not in C at this point, where we would just
force a block of memory to be interpreted as we want.

Regards

Anatol



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

Reply via email to