I updated to PHP 7.2 and it created an array (no pun intended) of issues. I’ve been knocking them out (mostly these sizeof and count() warnings. The one error we have :
Warning: sizeof(): Parameter must be an array or an object that implements Countable in /usr/www/domain/phpmyd/includes/class_registry.php on line 236
I tried fixing it like this :
if (sizeof($this->config) < 1) {
To this:
if (!empty($this->config) &&(sizeof($this->config) < 1)) {
But it creates lots more errors shown below, However, we fixed this one in the same way and it works perfectly. Changing this:
if (0 < sizeof($this->language)) {
To this:
if (!empty($this->language) && (0 < sizeof($this->language))) {
Took away basically the same error. Now, keep in mind, the above warning is the ONLY error left. Everything else works perfectly, however, if I do “fix” the warning, I get a bunch of errors that break the site and seem irrelevant. So, if I replace that first string all these errors appear:
- Warning: Use of undefined constant ADDON_DISCOUNT_CODES – assumed
‘ADDON_DISCOUNT_CODES’ (this will throw an Error in a future version
of PHP) in /usr/www/domainlistings/phpmyd/index.php on line 6 - Warning: Use of undefined constant ADDON_BLOG – assumed ‘ADDON_BLOG’
(this will throw an Error in a future version of PHP) in
/usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl
on line 134 - Warning: Use of undefined constant ADDON_LINK_CHECKER – assumed
‘ADDON_LINK_CHECKER’ (this will throw an Error in a future version of
PHP) in
/usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl
on line 179
Those errors did NOT appear, and those things worked perfectly well until I changed
if (sizeof($this->config) < 1) {
How is this linked? I’m not sure what is happening here, how that one line can make or break these other (seemingly irrelevant) things. Full code of inital problem (line 236):
/**
* Get a configuration value
* @param string $key
* @return mixed
*/
public function getConfig($key) {
if (sizeof($this->config) < 1) {
$this->loadConfig();
}
return isset($this->config[$key]) ? $this->config[$key] : false;
}
Any ideas?
4
Answers
For starters don’t use
sizeof
, butcount
😉 cheap improvement – always one opcode less.Secondly, make sure you pass an array, not null or whatever you have there, eg.:
To fix this properly you should never allow this property to be
null
anyway. If you’re after some lazy loading check if the variable is null or is not an array, eg:I think what’s happening here is that the change you originally made ensures that
loadConfig()
never happens, and something inloadConfig()
is responsible for defining all those constants you’re getting warned about after changing that.If you changed
to
then if
$this->config
wasnull
(the default value of an object property that has not yet been given a value), then the second one would mean the if condition wouldn’t be satisfied becausenull
is empty, where in the first one, it would be satisfied becausesizeof(null)
still returns 0 even though it gives you the uncountable warning.sizeof
was never really necessary there. You don’t have to count something just to see if it exists.I think this should work just fine, and make more sense for what it’s actually meant to do.
You can just check, before passing the variable, if it’s an array or not:
These days you can use the null coalescing operator (??) to help in this scenario; i.e. by using it to take the given value if it’s not null, or taken an empty array when the given value is null.
So your code:
if (sizeof($this->config) < 1) {
…would become:
if (sizeof($this->config ?? []) < 1) {
.