ID:          38508
 Updated by:  [EMAIL PROTECTED]
 Reported By: doublecompile at gmail dot com
-Status:      Open
+Status:      Closed
 Bug Type:    Feature/Change Request
-PHP Version: 5.2.0RC2
+PHP Version: *
 New Comment:

Why not simply have a method asArray() maybe even as par of an
interface:

interface ArrayConversion
{
  function asArray();
}

See, we have __toString as it is supported in language constructs like
echo, print and other internal functions. But we decided against an
autoconversion for arrays already. So itwill never be supported in any
language construct. That said there is no needed for this and nothing
you would win against the above interface. In fact you would make it
php more complex because you'd add just one more magic feature.


Previous Comments:
------------------------------------------------------------------------

[2006-08-19 00:40:17] doublecompile at gmail dot com

Description:
------------
Doing this:

$newarray = (array)$object;

will take the properties of an object and assign them as the values of
keys in an array.

As of PHP 5.2, doing this:

$stringified = (string)$object;

will call the magic __toString() function for a user-specified
formatting of the object as a string.

It would be a great addition to call a magic __toArray() function if an
object is cast as an array, instead of converting its public members to
array elements.  (For instance, the class might not have public
members).  Classes without the function could use the default method of
mapping property names to array keys.

Just my two cents.

Reproduce code:
---------------
<?php

class magicExample 
{
    public $aoeu = "htns";

    function __toArray()
    {
        return array('foo'=>'bar');
    }
}

$test = new magicExample();
$array = (array)$test;

print_r($array); // should show foo=>bar, not aoeu=>htns

?>



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=38508&edit=1

Reply via email to