From: wyattstorch42 at outlook dot com
Operating system: N/A
PHP version: Irrelevant
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:Other typecasting magic methods
Description:
------------
I am requesting other magic methods for automatic type conversion of
objects,
besides __toString(), in particular, __toArray(). The default method of
converting
an object to an array is, in my opinion, rather odd, since it provides the
values
of inaccessible properties and uses weird conventions, like an asterisk for
protected properties and prefixing the class name for private properties.
Test script:
---------------
class Example {
protected $a, $b, $c;
public function __construct ($a) {
$this->a = $a;
$this->b = 'B';
$this->c = 'C';
}
public function __toArray () {
// Only return a and b, and don't distinguish scope
return array (
'a' => $this->a,
'b' => $this->b
);
}
}
for ($examples = array (), $i = 0; $i < 3; $examples[] = (array) new
Example(++$i));
echo json_encode($examples, JSON_PRETTY_PRINT);
Expected result:
----------------
[
{
"a": 1,
"b": "B"
},
{
"a": 2,
"b": "B"
},
{
"a": 3,
"b": "B"
}
]
--
Edit bug report at https://bugs.php.net/bug.php?id=65553&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=65553&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=65553&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=65553&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=65553&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=65553&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=65553&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=65553&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=65553&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=65553&r=support
Expected behavior: https://bugs.php.net/fix.php?id=65553&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=65553&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=65553&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=65553&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65553&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=65553&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=65553&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=65553&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65553&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=65553&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=65553&r=mysqlcfg