From:             marcus at synchromedia dot co dot uk
Operating system: All
PHP version:      5.1.6
PHP Bug Type:     Class/Object related
Bug description:  Strange results or object to array cast

Description:
------------
When you cast an object to an array, and the object contains 
private or protected members, the resulting array keys are 
effectively corrupted.
Private members get the object class prepended to their 
name. Protected members get a '*' prepended to their name.
The docs say:
"If you convert an object to an array, you get the 
properties (member variables) of that object as the array's 
elements. The keys are the member variable names."
In reality, this is not true.

I don't see any real value in preserving access levels - 
arrays are not objects and they should not try to behave as 
them. You can find out the access level via introspection if 
you really need it, and by definition you have an instance 
handy to look at.

If it's intentional, it's not very helpful. As there's no 
separator between class name and variable name it's 
impossible to separate it correctly - if I had a property 
called 'Myclassfield1' in a Myclass instance, I would not be 
able to tell if it was a public property called 
'Myclassfield1' or a private property called 'field1'.

As this is deviating from documented behaviour and it's also 
fairly useless as it stands, I don't see any reason for 
keeping it like this.

Reproduce code:
---------------
<?php
class Myclass {
        public $field1 = '';
        private $field2 = '';
        protected $field3 = '';
}

$myclass = new Myclass;

var_dump($myclass);
var_dump((array)$myclass);
?>

Expected result:
----------------
object(Myclass)#1 (3) {
  ["field1"]=>
  string(0) ""
  ["field2:private"]=>
  string(0) ""
  ["field3:protected"]=>
  string(0) ""
}
array(3) {
  ["field1"]=>
  string(0) ""
  ["field2"]=>
  string(0) ""
  ["field3"]=>
  string(0) ""
}

Actual result:
--------------
object(Myclass)#1 (3) {
  ["field1"]=>
  string(0) ""
  ["field2:private"]=>
  string(0) ""
  ["field3:protected"]=>
  string(0) ""
}
array(3) {
  ["field1"]=>
  string(0) ""
  ["Myclassfield2"]=>
  string(0) ""
  ["*field3"]=>
  string(0) ""
}

-- 
Edit bug report at http://bugs.php.net/?id=38935&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=38935&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=38935&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=38935&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=38935&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=38935&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=38935&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=38935&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=38935&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=38935&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=38935&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=38935&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=38935&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=38935&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=38935&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=38935&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=38935&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=38935&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=38935&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=38935&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=38935&r=mysqlcfg

Reply via email to