[PHP-BUG] Req #75022 [NEW]: control over object interactions

[email protected] ("chris at kruining dot eu") Wed, 2 Aug 2017 09:28:01 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             chris at kruining dot eu
Operating system: All
PHP version:      Next Minor Version
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:control over object interactions

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 bug report at https://bugs.php.net/bug.php?id=75022&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75022&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75022&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75022&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75022&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75022&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75022&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75022&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75022&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75022&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75022&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75022&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75022&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75022&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75022&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75022&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75022&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75022&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75022&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75022&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75022&r=mysqlcfg