> 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. Aaron Piotrowski -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php