php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73321 array count shows wrong value when defined inside class
Submitted: 2016-10-14 11:57 UTC Modified: 2016-10-15 04:21 UTC
From: pmakkar at bayatree dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.6.26 OS: Mac OS X 10.11
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: pmakkar at bayatree dot com
New email:
PHP Version: OS:

 

 [2016-10-14 11:57 UTC] pmakkar at bayatree dot com
Description:
------------
I've a large PHP array (~85000 elements) in a class, on php 5.5 (/usr/bin/php) on my system (mac OS X 10.11) it works as expected (count ~ 86459), but on same system with php 5.6 (php), it fails to lookup few keys because of wrong count (count ~ 20923).

testAds.php prints city name corresponding to ID 1027519, which exists in 2nd file AdwordsCityComponent.php. But doesn't work with different PHP versions.


Test script:
---------------
class AdwordsCityComponent {
  private $adwordsCityObj = 
          array(
1000010=>"Abu Dhabi,Abu Dhabi,United Arab Emirates",
1000011=>"Ajman,Ajman,United Arab Emirates",
// ~80000 more rows....
9062586=>"Bratislava Region,Slovakia",
9062587=>"Trnava Region,Slovakia");

    /**
     * getCityNameById
     */
    public function getCityNameById($adwordsCityObjId) {
        $cityName = "Unspecified";
        $cityArray = $this->adwordsCityObj;

        echo "Total array count : ";
        echo count($this->adwordsCityObj);
        if(array_key_exists($adwordsCityObjId, $cityArray)){
            $cityName = $cityArray[$adwordsCityObjId];
        }
        return $cityName;
    }
}

full source code & php -i output at: https://www.dropbox.com/s/83sd89t58xn7kz2/PhpArrayIssue.zip?dl=0


Expected result:
----------------
// PHP 5.5 works fine
administratorsmac$ /usr/bin/php testAds.php 
Total array count : 64455
Bremerton,Washington,United States


Actual result:
--------------
// PHP 5.6 gives wrong count & result
administratorsmac$ php testAds.php 
Total array count : 31687
Unspecified


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-14 12:12 UTC] pmakkar at bayatree dot com
// PHP 5.5 works fine
administratorsmac$ /usr/bin/php testAds.php 
Total array count : 86459
Bremerton,Washington,United States


Actual result:
--------------
// PHP 5.6 gives wrong count & result
administratorsmac$ php testAds.php 
Total array count : 20923
Unspecified

Corrected actual results........
 [2016-10-14 14:23 UTC] requinix@php.net
-Summary: array count shows wrong value +Summary: array count shows wrong value when defined inside class -Type: Bug +Type: Documentation Problem -Package: Arrays related +Package: Class/Object related
 [2016-10-14 14:23 UTC] requinix@php.net
Duplicate of bug #68057.

Unfortunately this cannot be fixed in 5.6 due to binary BC, but you can work around it by defining the array outside of the class (and then somehow getting it into the class, if you wish).

I feel like this should be documented but I'm not sure where. OOP Changelog? Maybe in the 7.0 migration docs too?
http://php.net/manual/en/language.oop5.changelog.php
http://php.net/manual/en/migration70.php
 [2016-10-15 04:21 UTC] pmakkar at bayatree dot com
-Status: Open +Status: Closed
 [2016-10-15 04:21 UTC] pmakkar at bayatree dot com
Thanks requinix for your workaround, it works fine.

I'm closing this issue.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 03 06:01:29 2024 UTC