php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75022 control over object interactions
Submitted: 2017-08-02 09:27 UTC Modified: 2017-08-02 10:25 UTC
From: chris at kruining dot eu Assigned:
Status: Closed Package: *General Issues
PHP Version: Next Minor Version OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chris at kruining dot eu
New email:
PHP Version: OS:

 

 [2017-08-02 09:27 UTC] 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?',
    ]
]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
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
 [2017-08-02 10:23 UTC] chris at kruining dot eu
@requinix Thanks for the fast reply! I suspected this to be a duplicate request since I had seen a couple of the casting rfc's. Although I was hoping we could come up with a non-magic solution to implement this. but since the other ideas are much further along in the implementation as far as I'm concerned this issue can be closed :D
 [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
as this issue is duplicate and the one's it duplicate are much further along this issue can be closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jun 03 07:01:26 2025 UTC