|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-08-02 10:07 UTC] requinix@php.net
-Status: Open
+Status: Duplicate
-Package: PHP Language Specification
+Package: *General Issues
[2017-08-02 10:07 UTC] requinix@php.net
[2017-08-02 10:23 UTC] chris at kruining dot eu
[2017-08-02 10:25 UTC] chris at kruining dot eu
-Status: Duplicate
+Status: Closed
[2017-08-02 10:25 UTC] chris at kruining dot eu
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 08:00:02 2026 UTC |
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?', ] ]