[PHP7] Warning: count(): Parameter must be an array or an object that implements Countable in PHP 7.2

Created on 25 December 2017, almost 7 years ago
Updated 30 August 2024, 4 months ago

While install Drupal 8.4.3 in PHP 7.2 environment, this warning is occurred.

Warning: count(): Parameter must be an array or an object that implements Countable in Drupal\Core\Form\FormValidator->doValidateForm() (line 260 of core/lib/Drupal/Core/Form/FormValidator.php).
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 239)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'install_configure_form') (Line: 119)
Drupal\Core\Form\FormValidator->validateForm('install_configure_form', Array, Object) (Line: 571)
Drupal\Core\Form\FormBuilder->processForm('install_configure_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('install_configure_form', Object) (Line: 899)
install_get_form('Drupal\Core\Installer\Form\SiteConfigureForm', Array) (Line: 593)
install_run_task(Array, Array) (Line: 549)
install_run_tasks(Array) (Line: 117)
install_drupal(Object) (Line: 44)
🐛 Bug report
Status

Closed: duplicate

Version

8.4 ⚰️

Component
Base 

Last updated about 4 hours ago

Created by

🇯🇵Japan mh35

Live updates comments and jobs are added and updated live.
  • PHP 7.2

    The issue particularly affects sites running on PHP version 7.2.0 or later.

  • PHP 7.0

    The issue particularly affects sites running on PHP version 7.0.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇭🇰Hong Kong graham leach

    Hello,

    In my experience, this error is the result of insufficiently "defensive" programming.

    It occurs when PHP is asked to process something that lacks a *countable* property because what it is being presented with is NULL.

    Objects that are NULL do not have a countable property, which is the nature of the error/notice given.

    In previous versions of PHP, the [notice] error was not issued.

    But, as the quality of PHP improved, the [notice] warning was added somewhere in PHP 7.

    Here's an example of what would trigger that error:

    function element_children(&$elements, $sort = FALSE) {
      foreach ($elements as $key => $value) {
      }
    }
    

    Unfortunately, if the above function is passed a NULL array, a [notice] will be issued regarding the countable property.

    To avoid this, implement "defensive" programming akin to what is being done these days to validate form input (i.e. assume the worst) to "sanitize" it:

    function element_children(&$elements, $sort = FALSE) {
      if(!is_null($element) {
        foreach ($elements as $key => $value) {
        }
      }
    }
    

    Also, for those who might not know, using "&$elements" passes a reference to the variable as it exists in memory, not a copy of it. This is indicated by the "&" character.

    https://www.php.net/manual/en/language.references.pass.php

    For the purposes of this explanation you can ignore the "&" character in the function definition and simply consider it as "$elements".

    I have written a little paper on this aspect/shortcoming of Drupal Core. If you are interested to learn more, contact me.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
Production build 0.71.5 2024