Count() Emitting an E_Warning

count() emitting an E_WARNING

As we discussed, there are multiple ways to achieve the original functionality of count() and not emit an E_WARNING.

In PHP 7.3 a new function was added is_countable, specifically to address the E_WARNING issue and the prevalence of applications adopting is_array($var) || $var instanceof \Countable in their code.

In PHP 7.2, a Warning was added while trying to count uncountable
things. After that, everyone was forced to search and change their
code, to avoid it. Usually, the following piece of code became
standard:

if (is_array($foo) || $foo instanceof Countable) {
// $foo is countable
}

https://wiki.php.net/rfc/is-countable


Custom Function Replacement

For that reason it seems the best method of resolving the issue, is to perform the same functionality that PHP is doing with is_countable and creating a custom function to ensure compliance with the original functionality of count.

Example https://3v4l.org/8M0Wd

function countValid($array_or_countable, $mode = \COUNT_NORMAL)
{
if (
(\PHP_VERSION_ID >= 70300 && \is_countable($array_or_countable)) ||
\is_array($array_or_countable) ||
$array_or_countable instanceof \Countable
) {
return \count($array_or_countable, $mode);
}

return null === $array_or_countable ? 0 : 1;
}

Result

array: 3
string: 1
number: 1
iterator: 3
countable: 3
zero: 1
string_zero: 1
object: 1
stdClass: 1
null: 0
empty: 1
boolt: 1
boolf: 1

Notice: Undefined variable: undefined in /in/8M0Wd on line 53
undefined: 0

Shim is_countable() function

Using the above replacement function, it is also possible to shim is_countable in PHP <= 7.2, so it is only used when needed, with minimal overhead.

Example https://3v4l.org/i5KWH

if (!\function_exists('is_countable')) {
function is_countable($value)
{
return \is_array($value) || $value instanceof \Countable;
}
}

function countValid($array_or_countable, $mode = \COUNT_NORMAL)
{
if (\is_countable($array_or_countable)) {
return \count($array_or_countable, $mode);
}

return null === $array_or_countable ? 0 : 1;
}

Ignore count() Warnings

As the functionality of count() has not changed and not did not typically emit warnings in the past. An alternative to using a custom function, is to ignore the warning outright by using the @ Error Control Operator

Warning: This approach has the impact of treating undefined variables as NULL and not displaying Notice: Undefined variable:
message.

Example https://3v4l.org/nmWmE

@count($var);

Result

array: 3
string: 1
number: 1
iterator: 3
countable: 3
zero: 1
string_zero: 1
object: 1
stdClass: 1
null: 0
empty: 1
boolt: 1
boolf: 1
---
Undefined: 0

Replace count() using APD extension

As for replacing the internal PHP function count(). There is a PECL extension APD (Advanced PHP Debugger), that allows for override_function that works on core PHP functions. As the extension name suggests, it is technically meant for debugging, but is a viable alternative to replacing all instances of count for a custom function.

Example

\rename_function('count', 'old_count');
\override_function('count', '$array_or_countable,$mode', 'return countValid($array_or_countable,$mode);');

if (!\function_exists('is_countable')) {
function is_countable($value)
{
return \is_array($value) || $value instanceof \Countable;
}
}

function countValid($array_or_countable, $mode = \COUNT_NORMAL)
{
if (\is_countable($array_or_countable)) {
return \old_count($array_or_countable, $mode);
}

return null === $array_or_countable ? 0 : 1;
}

something about COUNT('')

From count() docs:

Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned.

count('') thus returns 1, just like count('seventeen') returns 1. Working as intended.

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

You can typo3 standard method
For example in your controller

$this->yourextRepository->findAll()->count();

May this will help you!

count(): Parameter must be an array or an object that implements Countable in php 7.2.1

The thing is you are checking for count > 1 with using ->one() which looks odd, looking at your code you want to return NULL if there is no record found and if you look into documentation the function one() already returns NULL if there are no records found so you are adding extra code and it could easily be reduced to

public static function findAdminByUsername($username)
{
return static::find()->where(
[
'username' => $username,
'userType' => 'ADMIN',
'user_status' => self::STATUS_ACTIVE,
]
)->one();
}

How to use is_countable to count if there is a row in the tbl that would equal to it?

Thank you very much hemnath mouli, Nick and Mr Shunz it is now working after changing the

if (is_countable($result)==1) {
$row = $result->fetch_array();
}

to

if (mysqli_num_rows($result)==1) {
$row = $result->fetch_array();


Related Topics



Leave a reply



Submit