php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43480 current,next,prev with objects
Submitted: 2007-12-02 22:20 UTC Modified: 2007-12-05 15:45 UTC
From: nicholsr@php.net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3CVS-2007-12-02 (snap) OS: all
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nicholsr@php.net
New email:
PHP Version: OS:

 

 [2007-12-02 22:20 UTC] nicholsr@php.net
Description:
------------
The array functions current, next,prev and possibly others no longer work on objects on php5.3.

The expected result below was obtained on php 5.2.

I noticed that at least one of the testcases were changed in order to avoid calling out this change in behaviour so i wondered if it was deliberate. 

Specifically, ext/standard/tests/array/009.phpt in PHP 5.2 will check for object behaviour but this was removed in 5.3. 

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

class myclass {

public $prop1="prop1";
public $prop2="prop2";

};

$obj=new myclass();

var_dump (current($obj));
next ($obj);
var_dump (current($obj));
?>

Expected result:
----------------
string(5) "prop1"
string(5) "prop2"

Actual result:
--------------
Warning: current() expects parameter 1 to be array, object given in 1.php on line 12
NULL

Warning: next() expects parameter 1 to be array, object given in 1.php on line 13

Warning: current() expects parameter 1 to be array, object given in 1.php on line 14
NULL


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-02 22:22 UTC] johannes@php.net
Jani, I'd say it's related to your zend_parse_parameters MFH'ing.
 [2007-12-02 22:24 UTC] tony2001@php.net
The change makes sense to me.
Using array funcs with objects is quite useless feature that should be dropped IMO.

 [2007-12-02 22:48 UTC] derick@php.net
I don't see why we should unnecessarily break BC for this though..
 [2007-12-02 22:50 UTC] nicholsr@php.net
I do not have an opinion on whether this behaviour should be dropped. I just noticed that the behaviour had changed. I asked Johannes if this was a deliberate decision and he advised me to open this bug.  I dont know the source of enough PHP apps to know if this is likely to be a major BC hit moving from PHP 5.2 to 5.3. I dont know how common this idiom is.
 [2007-12-02 22:57 UTC] tony2001@php.net
The BC break is minimal, if at all.
I've never heard of such usage before.
 [2007-12-03 15:56 UTC] jani@php.net
This was merely MFH'd. If it's wrong in HEAD, that should be dealt with. I have nothing to do with how this works in HEAD.
 [2007-12-04 05:58 UTC] crrodriguez at suse dot de
I see nothing wrong with this new behaviuor, the old one looks pretty much broken.
 [2007-12-04 12:34 UTC] jani@php.net
And neither do I. Sometimes things get fixed with new versions.. ;)
 [2007-12-04 18:41 UTC] stas@php.net
Similar, if not exactly the same, behavior is documented in the manual:
http://www.php.net/manual/en/language.oop5.iterations.php

and I can see no compelling reason to forbid doing the same with functions. Is there one? I think it's too early to close this one.
 [2007-12-05 15:45 UTC] jani@php.net
All these functions expect an array as parameter and that is also how it's been documented. It was only a side-effect that it worked for objects. And that document page you refer to (and the iterator there) is for _arrays_ not objects.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 17:01:33 2025 UTC