Req #75022 [Opn->Dup]: control over object interactions

From: Date: Wed, 02 Aug 2017 10:07:48 +0000
Subject: Req #75022 [Opn->Dup]: control over object interactions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210451@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75022&edit=1 ID: 75022 Updated by: requinix@php.net Reported by: chris at kruining dot eu Summary: control over object interactions -Status: Open +Status: Duplicate Type: Feature/Change Request -Package: PHP Language Specification +Package: *General Issues Operating System: All PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: Those ideas have been discussed multiple times in the past. Last I knew, operator overloading was looked down upon (it obscures code and creates magical behavior) while casting didn't have a satisfactory implementation (due to it having so many types to support casting to). The latter is the only one that ever made it to an RFC - though barely as it's only a draft. https://wiki.php.net/rfc/object_cast_to_types Look through previous requests here and the PHP internals mailing list to learn more. https://marc.info/?l=php-internals http://php.markmail.org/search/?q=list:net.php.lists.internals You can re-raise the subjects on the list, of course, but please make sure you understand the existing history for both requests first. http://php.net/mailing-lists.php Previous Comments: ------------------------------------------------------------------------ [2017-08-02 09:27:58] chris at kruining dot eu Description: ------------ I know that this subject is somewhat controversial in the community, nevertheless I want to request this as an language feature; The ability to gain control over object interactions. In this case I mean both casting and operator interactions. I have no clue or suggestion how this would work syntax-wise. One example would be to copy the c++ style of operator overloading, which is in my honest opinion not an elegant solution. the other one would be called cast-overloading I guess. In my case I have a Collection class, which is just a glorified array with a couple of options to chain methods, with the ArrayAccess and IteratorAggregate I can interact with it as if it ware an array if I wish to do so, however whenever I want to pass the collection to a function/method who takes an array as parameter I have to call the ToArray method I had to implement, which all it does is return the array it wraps. I would love to have control over how my object is cast to scalar types, in this case object to array, but I can think of many cases where I want to 'cast' to an integer or float. I am very curious to what you(the community) think of this proposal. Because on a more abstract level what I propose is to gain control over implicit code instead of making my code explicit. Test script: --------------- - Expected result: ---------------- $collection = Collection::From([ 'these', 'are', 'some', 'items' ]); $collection[] = ','; $collection[] = 'are'; $collection[] = 'they'; $collection[] = 'not?'; $collection =(array)$collection; Would be [ 'these', 'are', 'some', 'items', ',', 'are', 'they', 'not?' ] Actual result: -------------- Actually is [ 'items' => [ 'these', 'are', 'some', 'items', ',', 'are', 'they', 'not?', ] ] ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75022&edit=1

« previous php.bugs (#210451) next »