Edit report at https://bugs.php.net/bug.php?id=38508&edit=1
ID: 38508
Comment by: webdes87 at gmail dot com
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:
+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.
Previous Comments:
------------------------------------------------------------------------
[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!
------------------------------------------------------------------------
[2013-05-24 03:39:23] rich dot remer at gmail dot com
I think the main benefit it offers is the ability to control what happens during
a cast operation. Right now, casting simple scalar values or NULL to an array
works as expected.
While it's possible to cast an object to an array, the semantics of what should
happen in this situation are not nearly as clear. This really should be
controlled by the class.
------------------------------------------------------------------------
[2013-03-07 12:19:14] ante at caan dot si
Hi guys. I'm dragging this out from the "History". I think this is a great suggestion
as I use a lot of object that implement ArrayAccess so doing this
$someArray = (array) $obj;
that calls $obj->__toArray() would be a GREAT addition to PHP logic.
------------------------------------------------------------------------
[2012-03-14 18:55:00] erck0006 at gmail dot com
// MY APPLICATION'S INTERIM SOLUTION (**UPDATE**)
$customers = new Customers();
$customers = $customers->__toArray(); // THIS LINE IS THE **UPDATED** LINE NEEDED IN ORDER TO
AVOID TRIGGERING THE FOLLOWING ERROR ON THE FOLLOWING LINE: Only variables should be passed by
reference
$lastCustomer = array_pop($customers); // SUCCESS
echo $lastCustomer->getName(); // prints the last customer's name since the previous line
did not fail since I explicitly called a custom method named __toArray() first
------------------------------------------------------------------------
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