Req #52807 [Dup->Opn]: magic __toArray() for objects

From: Date: Sun, 12 Sep 2010 02:06:35 +0000
Subject: Req #52807 [Dup->Opn]: magic __toArray() for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-154264@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
 Updated by:         aharvey@php.net
 Reported by:        jtegwen at gmail dot com
 Summary:            magic __toArray() for objects
-Status:             Duplicate
+Status:             Open
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   n/a
 PHP Version:        Irrelevant
 Block user comment: N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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.

------------------------------------------------------------------------
[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


Thread (13 messages)

« previous php.bugs (#154264) next »