Re: Updating legacy code ...
| From: | AshleySheridan | Date: | Mon, 04 Mar 2019 19:57:37 +0000 |
| Subject: | Re: Updating legacy code ... | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326731@lists.php.net to get a copy of this message | ||
On Mon, 2019-03-04 at 18:59 +0000, Lester Caine wrote:
> On 04/03/2019 16:59, Jonesy via php-general wrote:
> > On Mon, 4 Mar 2019 15:55:30 +0000, Lester Caine wrote:
> > > On 04/03/2019 15:18, Aziz Saleh wrote:
> > > > http://php.net/manual/en/function.count.php
> > > >
> > > > 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. There is one exception, if
> > > > array_or_countable is NULL, 0 will be returned.
> > > >
> > > > If the item is null, it should indeed return 0.
> > >
> > > Except since 7.2.0 it issues a warning as well!
> > > Have to do
> > > echo count((array)$item);
> > > to shut it up ...
> >
> > Isn't it just: @count($item) ???
>
> We are told that '@' will be dropped at some point? But perhaps it
> IS
> the right solution to fix what simply looks like a mistake anyway.
>
> What I AM trying to establish is just what is the 'correct' way to
> do
> these things since it is obvious that the methods used for 20+ years
> are
> no longer acceptable? @ may work as a fix today, but for PHP8?
>
> Although I'm tempted to nail down PHP7.2 in much the same way I used
> PHP5.2 for over 10 years and by then I'm well into retirement and
> can
> leave it to someone else to do the next re-write :) So perhaps
> 'count('
> -> '@count(' may be the correct solution. The feedback on the Smarty
> forums suggest simply rolling back to 7.1!
>
I think most people will agree, using @ to suppress errors is "Not A
Good Idea"™. It's also recommended to avoid running with error display
on in production environments, but perhaps that's the only way to
reliably test with real data? I think we'd all agree that it's rare
that a staging/dev environment is a 100% replica of production.
Having said that, you might be able to turn the level of errors down to
prevent warnings from being shown. While this will only hide the
problem, warnings tend to be limited to things that won't break your
application if they don't work entirely as expected.
Without knowing much about your application, this is only a suggestion,
but is it possible to replace your arrays with another kind of object
that can be iterated? This way, you could have empty instances that can
behave themselves better, the empty checks can be moved to the object
methods that require them, and your data objects can move towards a
more readable and future-proofed structure.
Thanks,
Ash
http://www.ashleysheridan.co.uk