php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72234 Calling foreach on non-array is ignored
Submitted: 2016-05-17 20:13 UTC Modified: 2016-05-17 23:29 UTC
From: nickdnk at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.6 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nickdnk at hotmail dot com
New email:
PHP Version: OS:

 

 [2016-05-17 20:13 UTC] nickdnk at hotmail dot com
Description:
------------
Hello

If passing a variable (let's say a stdClass) to the foreach method, it does not produce an error. It silently does not loop the object (since it cannot).

I'm no expert, but I'd say that at least a warning should be raised if trying to iterate a non-array object. I'm sorry if this is intentional, but I do believe an error was raised in 5.6.


Test script:
---------------
$obj = new stdClass();

foreach ($obj as $anObj) {
    error_log("nothing is output here, obviously, but no error either");
}

Expected result:
----------------
I expect an error or warning to occur, since I'm traversing a non-array.

Actual result:
--------------
Nothing happens at all.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-17 20:33 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-05-17 20:33 UTC] requinix@php.net
foreach on an object is allowed. You aren't seeing anything because the $obj is empty.
http://php.net/manual/en/control-structures.foreach.php
https://3v4l.org/CCrFo
 [2016-05-17 22:29 UTC] nickdnk at hotmail dot com
But, the link clearly states (for the first form, which is what I used as example): "It [The first form] loops over the *array* given by *array_expression*. On each iteration, the value of the current element is assigned to $value..."

 - when not iterating an array, why are we not seeing at least a warning? Would there ever be a situation where you'd legitimately use foreach on a non-array object? If so please enlighten me. Not a single one of the examples in the documentation for foreach use a non-array object.
 [2016-05-17 22:39 UTC] requinix@php.net
-Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: Scripting Engine problem
 [2016-05-17 22:39 UTC] requinix@php.net
If you have ideas on how to make the documentation more clearly indicate that foreach does, in fact, work on objects then feel free to hit the Edit link in the top-right corner of the page and submit whatever changes you think are necessary.
 [2016-05-17 23:15 UTC] nickdnk at hotmail dot com
I don't know how to edit the documentation to alleviate this problem as I don't know when or why you'd ever knowingly use foreach on a non-array object, hence the original bug report.

Consider the following:

class SomeClass {

    private $anArray;

    public function getArray() {
        return $this->anArray;
    }

}

I left out constructor etc, but assume the private variable contains a valid array, that our class has been instantiated and that it is referenced by a variable, let's call it $ourClass.

Now somewhere in our code we *accidentally* do this:

    foreach ($ourClass as $anObject) {
        *do something with $anObject*
        // Nothing happens
    }

Instead of the correct approach:

    foreach ($ourClass->getArray() as $anObject) {
        *do something with $anObject*
        // Something happens
    }

Now our code silently fails. I realize this is a programmer's error, but are warnings and such not meant to help us avoid errors that are *always* errors? That's why I wanted an example of when the first approach is ever sensible, as in; is it ever not an error? If not, why not warn the user that he is doing something senseless? If yes, please provide an example. Or does it have something to do with speed - as in having to check for this every time foreach is used?

Thanks :)
 [2016-05-17 23:29 UTC] requinix@php.net
It's fine that you don't want to iterate over regular objects. I don't do it either; I'd probably use an array or implement Iterator/IteratorAggregate instead. But just because we don't do it doesn't mean other people do not.

foreach supports objects. That will not change anytime soon. If you want to change it then you need to use the RFC process. https://wiki.php.net/rfc/howto If instead you think of a way to clarify what's written in the documentation, I've already told you about the Edit link. Otherwise there is nothing to do here.
 [2016-05-17 23:34 UTC] nickdnk at hotmail dot com
Okay, fair enough. I'll see if I can provide something to the documentation to make this more clear to everyone.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 02:01:33 2024 UTC