php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30731 SimpleXML objects should implement the Countable interface
Submitted: 2004-11-09 10:15 UTC Modified: 2004-11-10 22:45 UTC
From: jarkko dot laiho at iki dot fi Assigned: helly (profile)
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.* OS: *
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: jarkko dot laiho at iki dot fi
New email:
PHP Version: OS:

 

 [2004-11-09 10:15 UTC] jarkko dot laiho at iki dot fi
Description:
------------
SimpleXML objects should implement the Countable interface in order to work properly with the count() function.

The comments of bug #30113, "ArrayAccess interface does not implement count functionality", imply that the Countable interface appears in PHP 5.1 and is not yet available in 5.0.2. Therefore, the behaviour of the repro code below (returning 1 for count() instead of 3) is not a bug for 5.0.2, since it is expected behaviour for count() to return 1 when given an object as its parameter. However, given the pseudo-array nature of a SimpleXML object, a count() of 3 would be very convenient indeed in many situations.

I was unable to find a mention of Countable getting implemented for SimpleXML objects in 5.1, so I'm submitting this request for consideration.

(Note: in the code below, there would naturally be trivial ways to get a proper count for the $xml->wibblet->somesuch elements, but the code is meant to merely illustrate the described issue.)

Reproduce code:
---------------
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<testdata>
  <wibblet>
     <somesuch>random data</somesuch>
     <somesuch>more random data</somesuch>
     <somesuch>not that random</somesuch>
  </wibblet>
</testdata>
XML;
$xml = simplexml_load_string($xmlstr);
foreach($xml->wibblet->somesuch as $somesuch) {
   echo $somesuch;
   echo "<br />";
}
echo "Count: " . count($xml->wibblet->somesuch);
?>

Expected result:
----------------
random data
more random data
not that random
Count: 3

Actual result:
--------------
random data
more random data
not that random
Count: 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-10 22:45 UTC] helly@php.net
We do not know whether to count elements or arrays and we also do not know whether to count recursive or not. Thus we cannot implement Countable.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 10:01:32 2024 UTC