php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44168 Unusual output when using each() on private/protected properties of an object
Submitted: 2008-02-19 14:09 UTC Modified: 2008-02-19 14:14 UTC
From: jmessa@php.net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2CVS-2008-02-19 (CVS) OS: Windpws XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jmessa@php.net
New email:
PHP Version: OS:

 

 [2008-02-19 14:09 UTC] jmessa@php.net
Description:
------------
When using the each() function on an object that contains private or protected properties I'm getting some unusual output shown below. 
I was wondering whether each() should be behaving the same as foreach() does (as shown in the documentation here: http://www.php.net/manual/en/language.oop5.iterations.php) and not be able to access private/protected properties, or whether like the next, prev and current functions, each() should not work with objects at all (bug raised here: http://bugs.php.net/bug.php?id=43480).

Reproduce code:
---------------
class myClass {
	private $val1 = 'hello';
	protected $foo = 'world';
	var $bar = 'baz';
}
$input = new myClass;

var_dump($input);

var_dump( each($input) );
var_dump( each($input) );
var_dump( each($input) );

Expected result:
----------------
array(4) {
  [1]=>
  string(3) "baz"
  ["value"]=>
  string(3) "baz"
  [0]=>
  string(3) "bar"
  ["key"]=>
  string(3) "bar"
}

Actual result:
--------------
array(4) {
  [1]=>
  string(5) "hello"
  ["value"]=>
  string(5) "hello"
  [0]=>
  string(13) " myClass val1"
  ["key"]=>
  string(13) " myClass val1"
}
array(4) {
  [1]=>
  string(5) "world"
  ["value"]=>
  string(5) "world"
  [0]=>
  string(6) " * foo"
  ["key"]=>
  string(6) " * foo"
}
array(4) {
  [1]=>
  string(3) "baz"
  ["value"]=>
  string(3) "baz"
  [0]=>
  string(3) "bar"
  ["key"]=>
  string(3) "bar"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-19 14:14 UTC] felipe@php.net
Hi Josie. I already have reported that.

http://bugs.php.net/bug.php?id=43713

Derick' article about this:
http://derickrethans.nl/private_properties_exposed.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 16:01:30 2024 UTC