Req #38508 [Com]: Addition of Magic __toArray() function

From: Date: Thu, 12 Mar 2020 16:03:52 +0000
Subject: Req #38508 [Com]: Addition of Magic __toArray() function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226066@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=38508&edit=1

 ID:                 38508
 Comment by:         john at designermonkey dot co dot uk
 Reported by:        doublecompile at gmail dot com
 Summary:            Addition of Magic __toArray() function
 Status:             Closed
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 PHP Version:        *
 Block user comment: N
 Private report:     N

 New Comment:

I agree with the above comment that implicit array conversion is already supported in the language,
and is not very functional for a programmer as it relies on logic that the programmer has no access
to.

Adding __toArray as a magic method would solve all sorts of issues for object
serialization in large object trees for example.

I guess the JsonSerializable interface already implements the logic, although it
can't be used outside of json_encode, so why not make it simple using
__toArray and __toString?


Previous Comments:
------------------------------------------------------------------------
[2018-11-07 17:38:20] jean dot claveau at gmail dot com

Isn't it time to make an RFC and submit it to a vote instead of relying
on a 10 years old opinion?

https://wiki.php.net/rfc

Having something like __toArray() would make OOP collections compatible
with https://secure.php.net/manual/fr/function.array-filter.php
as it
doesn't take a Traversable as parameter but only a true array.

Just an example :)

__toBool() would be super interesting also, especially for logic
handling

------------------------------------------------------------------------
[2017-12-14 14:10:17] MUST at BE dot VALID

+1 - 15 years later this is still needed :)

------------------------------------------------------------------------
[2016-07-13 12:35:44] webdes87 at gmail dot com

+1
Would also vote for this. Makes absolute sence since (array) cast syntax exist already in the
language construction.

Maybe also support other castings that can be called like $var = (xxx) $obj
as __toXXX

would enhance the language by a reliable construct, that does not only work on several types like
__toString

Would allow also goodies like __toLogEntry where LogEntry is some other Object etc.

------------------------------------------------------------------------
[2014-03-10 16:18:16] omidmr at gmail dot com

+1
And all other type casting magic methods.

------------------------------------------------------------------------
[2013-10-05 16:42:24] lukas at lu-x dot me

I can only +1 the opinion from the last comment.
Magic methods make PHP so powerful. So why limit it to string casting?

The creator of the class should decide what values to return in which case - the user could just use
the object in different ways without thinking about what methods would return what. It would all
work automatically - using an array function would ask the object to return an array, using a string
function would ask to return a string etc.

So please implement magic methods for all data types in PHP - or, if that adds "more
complexity", add interfaces for that - the possibility to define custom conversion rules just
has to exist!

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=38508


--
Edit this bug report at https://bugs.php.net/bug.php?id=38508&edit=1


Thread (14 messages)

« previous php.bugs (#226066) next »