Req #52807 [NEW]: magic __toArray() for objects

From: Date: Thu, 09 Sep 2010 20:33:54 +0000
Subject: Req #52807 [NEW]: magic __toArray() for objects
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-154221@lists.php.net to get a copy of this message
From:             
Operating system: n/a
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:magic __toArray() for objects

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 bug report at http://bugs.php.net/bug.php?id=52807&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=52807&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=52807&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=52807&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=52807&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52807&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=52807&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=52807&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=52807&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=52807&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=52807&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=52807&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=52807&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=52807&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=52807&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52807&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52807&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=52807&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=52807&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=52807&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=52807&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=52807&r=mysqlcfg



Thread (13 messages)

« previous php.bugs (#154221) next »