Req #52807 [Com]: magic __toArray() for objects
| From: | jtegwen at gmail dot com | Date: | Fri, 10 Sep 2010 15:21:11 +0000 |
| Subject: | Req #52807 [Com]: magic __toArray() for objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-154246@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52807&edit=1
ID: 52807
Comment by: jtegwen at gmail dot com
Reported by: jtegwen at gmail dot com
Summary: magic __toArray() for objects
Status: Duplicate
Type: Feature/Change Request
Package: Class/Object related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
New Comment:
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?
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[2010-09-09 22:33:53] jtegwen at gmail dot com
Description:
------------
It would be nice to have an analogous method to __toString() for
arrays.. maybe __toArray() for customized array return.
Test script:
---------------
class myObject {
private $data1;
private $data2;
private $meta1;
public function __construct($data1, $data2) {
$this->data1 = $data1;
$this->data2 = $data2;
$this->meta1 = strlen($data1) + strlen($data2);
}
public function __toArray() {
return array('data1'=>$this->data1, 'data2'=>$this->data2);
}
}
$obj = new myObject('test', 'script');
var_dump($obj);
Expected result:
----------------
array('data1'=>'test', 'data2'=> 'script')
Actual result:
--------------
Not implemented
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52807&edit=1