count(); PHP Warning on PHP 7.2 and above

  • Hi,

    I am building a shop which uses Cart Properties however when the server is using PHP 7.2 or higher it gives this PHP warning when using perch_shop_cart()

    Warning: count(): Parameter must be an array or an object that implements Countable in XXXXXXXX/addons/apps/perch_shop/lib/PerchShop_Cart.class.php on line 516

    After reading this threads here I changed line 516 in /addons/apps/perch_shop/lib/PerchShop_Cart.class.php from

    if (count($json)) { to if (PerchUtil::count($json)) {

    This clears the PHP warning, 2 questions, is this okay and should this be in a future update?

  • Jason

    Changed the title of the thread from “perch_shop_cart() can cause PHP Warning on PHP 7.2 and above” to “count(); PHP Warning on PHP 7.2 and above”.
  • I have also noticed this problem / error on the Shop Dashboard, see attached, when upgrading a clients PHP version.

    The only difference between no warning and warning was the PHP version 7.1 vs 7.3. I have fixed the issue and tested / all seems to be working.

    On addons>perch_shop>modes> I changed count($data['items']) on lines 208 & 209 to PerchUtil::count($data['items'])

    This fixes the issue. The reason I believe is after PHP 7.2 the count function changed in behaviour. See here:…57945/php-7-2-count-error

    Should this be in the next release of Perch Shop? drewm previous comment here tells me it should?


    "Ah, good call. Most count() calls go through PerchUtil::count(), which should protect against this. That was a rare one which didn't.'"

  • Hi Drew,

    I've run into the same problem as Jason, I've liked both his comments above (to hopefully raise their visibility), however the reality is that "I don't like this". The lack of even maintenance updates to rectify problems such as this is alarming. I've spent many months building a new site for a client, reliant on the Perch platform and the Shop app to take their business online… is a huge step for them and an important project for me They've put their faith in me to guide and advise them on the platform that they should use. Given the current state of stagnation, the lack of any clear roadmap or communication (despite the concerns raised by othhers on this forum) I feel like a schmuck… a fraudster… this is not right!

    The current situation is not good for your business… my business… or my client's business.


  • I agree with everything mentioned by ndwl .

    But to keep your shop up to date with current PHP development you can fix this error quite easily:

    1. foreach($order as $widget_id) {
    2. foreach($stats as $widget=>$data) {
    3. if ($widget==$widget_id) {
    4. if (isset($data['items']) && is_array($data['items']) && count($data['items'])) {
    5. $found_items += count($data['items']);
    6. }
    7. $dashboardHtml .= $Stats->render_widget($widget, $data);
    8. }
    9. }
    10. }

    The warning raised is because $data['items'] can be false which is a boolean not an object that implements the Countable interface, so with recent changes to PHP trying to count a boolean throws this warning. We can "patch" this issue on line 208 of perch_shop/modes/ by adding is_array($data['items']) condition. So it will only count if $data['items] is actually countable.