php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41678 ArrayObject::getArrayCopy() gives strange results
Submitted: 2007-06-13 12:10 UTC Modified: 2007-06-16 08:08 UTC
From: killgec at gmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.2.3 OS: *
Private report: No CVE-ID: None
 [2007-06-13 12:10 UTC] killgec at gmail dot com
Description:
------------
ArrayObject::getArrayCopy() returns not only public properties of an object, but private and protected properties, too. These last two have strange "inner-style" keys with illegal characters. 

Documentation says that ArrayObject::getArrayCopy() returns array created of public properties: http://www.php.net/~helly/php/ext/spl/classArrayObject.html

Reproduce code:
---------------
class A {
	public $a = 1;
	protected $b = 2;
	private $c = 3;
	
}

$a = new A();
$ao = new ArrayObject($a);
print_R($ao->getArrayCopy());
var_dump((array)$ao);

Expected result:
----------------
Array
(
    [a] => 1
)
array(3) {
  ["a"]=>
  int(1)
}

Actual result:
--------------
Array
(
    [a] => 1
    [< 1 illegal character here>*< 1 illegal character here>b] => 2
    [< 1 illegal character here>A< 1 illegal character here>c] => 3
)
array(3) {
  ["a"]=>
  int(1)
  ["< 1 illegal character here>*< 1 illegal character here>b"]=>
  int(2)
  ["< 1 illegal character here>A< 1 illegal character here>c"]=>
  int(3)
}

literally:

Array
(
    [a] => 1
    [&#65533;*&#65533;b] => 2
    [&#65533;A&#65533;c] => 3
)
array(3) {
  ["a"]=>
  int(1)
  ["&#65533;*&#65533;b"]=>
  int(2)
  ["&#65533;A&#65533;c"]=>
  int(3)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-15 21:15 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

These strange chars are actually zero's. And that is simply the way PHP encodes public and protected member variables.
 [2007-06-16 08:08 UTC] killgec at gmail dot com
Thanks. Then I think docs should be corrected:

"when the ArrayObject refers to an object then this method returns an array of the public properties."

http://www.php.net/~helly/php/ext/spl/classArrayObject.html#0215024a956996b2ddec156e0d7d544a
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 20:01:45 2024 UTC