PHP: Ab PHP7 Fehler bei count()

edit | delete

Autor: Ralf v.d.Mark

eingetragen: Donnerstag, 05. Dezember 2019 um 10:44 Uhr (49/2019 Kalenderwoche)

geändert: Dienstag, 04. Februar 2020 um 13:42 Uhr (6/2020 Kalenderwoche)

Keywords: count iscountable is_countable array object

Kategorien: PHP-ZF, PHP,

Text:

Der Wechsel auf PHP-7 führt oft zum Fehler bei "count()"!


Warning: count(): Parameter must be an array or an object that implements Countable in dateiname.phtml on line 4711


Da aber die Frage, ob man das überhaupt zählen kann [is_countable()] erst mit PHP-7.3 eingeführt wurde, kann man zwischenzeitlich einen kleinen Hack (s. u. LÖSUNG) einbauen.


Beispiel für die Klasse ist das ZF1-Projekt "AIS 2":
/application/components/Helper/CountWithCountable.php

Quellcode:  

if (count($buttonBoxArr) > 0) {
    ?>$buttonBoxArr ist größer als 0<?php
}
// Wenn der Parameter nicht zählbar ist, kommt eine Warnung!


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
//LÖSUNG:

//Folgende IF-Abfrage und Funktion ist ab PHP-7.3 überflüssig!
if (!function_exists('is_countable')) {
    function is_countable($var) {
        return (is_array($var) || $var instanceof Countable);
    }//ENDE: function is_countable(...) 
}//ENDE: if (!function_exists('is_countable')) 


if (is_countable($buttonBoxArr) && count($buttonBoxArr) > 0) {
    ?>$buttonBoxArr ist größer als 0<?php
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
//Noch eleganter ist es vielleicht mit dieser Klasse:
class Helper_CountWithCountable
{
    /**
     * Nachfrage, ob man das überhaupt zählen kann [is_countable()]
     * und wenn ja den Integer zurückgeben, anderfalls 0.
     *
     * PHP version 7
     *
     * @name       static public Methode "getCount()"
     * @author     Ralf von der Mark, Ref. 223 (Entwicklung), BLE
     *
     * @param array|object|?  $zuZaehlenderWert
     *
     * @return integer (Wenn Wert nicht zählbar oder leer, kommt 0 zurück)
     *
     * @example
     *  if (Helper_CountWithCountable::getCount($buttonBoxArr) > 0) {...}
     *  macht das gleiche wie:
     *  if (is_countable($buttonBoxArr) && count($buttonBoxArr) > 0) {...}
     *
     * Siehe in Projekt AIS (ZF1) in Datei:
     *    /application/components/Helper/CountWithCountable.php
     */
    static public function getCount($zuZaehlenderWert)
    {

        //Folgende IF-Abfrage und Funktion ist ab PHP-7.3 überflüssig!
        if (!function_exists('is_countable')) {
            function is_countable($var) {
                return (is_array($var) || $var instanceof Countable);
            }//ENDE: function is_countable(...)
        }//ENDE: if (!function_exists('is_countable'))

        if (is_countable($zuZaehlenderWert) && count($zuZaehlenderWert) > 0) {
            return $zuZaehlenderWert;
        } else {
            //Wenn nicht zählbar oder exsistiert dann 0
            return 0;
        }//ENDE: else ==> if (!empty())

    }//ENDE: function getCount(...)
    
}//ENDE: class()