Edit report at http://bugs.php.net/bug.php?id=52807&edit=1
ID: 52807 Comment by: + at ni-po 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 New Comment: @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? Previous Comments: ------------------------------------------------------------------------ [2010-09-12 04:06:35] ahar...@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] ahar...@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