Req #52807 [Com]: magic __toArray() for objects
| From: | umpirsky at gmail dot com | Date: | Mon, 24 Sep 2012 12:50:53 +0000 |
| Subject: | Req #52807 [Com]: magic __toArray() for objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-173681@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=52807&edit=1
ID: 52807
Comment by: umpirsky at gmail dot com
Reported by: jtegwen at gmail dot com
Summary: magic __toArray() for objects
Status: Open
Type: Feature/Change Request
Package: Class/Object related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
@dmgx This is not the problem the bug is about. What if you have some library
which casts variables to arrays, like Zend_XMLRPC for example. Iterator or
ArrayAccess will not do the job, since you need control over casting your class to
array.
Previous Comments:
------------------------------------------------------------------------
[2010-10-11 17:17:20] dmgx dot michael at gmail dot com
I've found that when I have an object I need to behave like an array in some contexts that it
is worth the time to implement Iterator and Array in the object, which allows the object to behave
like an array. It takes more effort than implementing a theoretical magic __toArray() method, but
its worth it.
Read here for information on doing this: http://php.net/manual/en/language.oop5.iterations.php
------------------------------------------------------------------------
[2010-10-03 18:36:00] + at ni-po dot com
@aharvey: Couldn't the patch in the RFC you mentioned be modified to support arrays, too? Or
would the implementation for arrays be completely different to the one for arrays?
------------------------------------------------------------------------
[2010-09-12 04:06:35] aharvey@php.net
You are quite correct about request #52583. My apologies. I
remember skimming it when it came in, but obviously not very well. :)
To be honest, I think this is a better candidate for a thread on the
Internals mailing list or an RFC than a feature request here. I also
think this is extremely unlikely to make it in in any case,
particularly without a patch.
Nevertheless, I'll reopen this for now.
------------------------------------------------------------------------
[2010-09-10 17:21:11] jtegwen at gmail dot com
I found that request when researching this one. I read that as casting *to* an object not casting
*from* an object.
Am I missing something?
------------------------------------------------------------------------
[2010-09-10 04:58:39] aharvey@php.net
Probably better bundled under request #52583 -- the RFC linked to there is specifically to deal with
casting to scalar types, but the syntax suggested in the feature request is general enough to deal
with arrays.
------------------------------------------------------------------------
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=52807
--
Edit this bug report at https://bugs.php.net/bug.php?id=52807&edit=1