Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18961

Re: php 7.4: null["key"]

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From "J.O. Aho" <user@example.net>
Newsgroups comp.lang.php
Subject Re: php 7.4: null["key"]
Date Mon, 30 May 2022 08:06:01 +0200
Lines 68
Message-ID <jfj569Foau1U1@mid.individual.net> (permalink)
References <86mtfcq6yj.fsf@levado.to> <86ilpnepih.fsf@levado.to>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net 4jH7nFF6q0g5O9sqmmhf4ARgcEaLDz7zfTLW3jePN4w+ax+okM
Cancel-Lock sha1:MAJbSquqs0rGMyrEO31hijbddG8=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0
Content-Language en-US-large
In-Reply-To <86ilpnepih.fsf@levado.to>
Xref csiph.com comp.lang.php:18961

Show key headers only | View raw


On 30/05/2022 04.13, Meredith Montgomery wrote:
> Meredith Montgomery <mmontgomery@levado.to> writes:
> 
>> I'm upgrading code to PHP 7.4, which will now blow a notice for things
>> such as
>>
>>    null["key"]
>>    (1.2)["key"]
>>    (123)["key"]
>>
>> All of these seem to reduce to null.  (The footnote explains why I think
>> so.)
>>
>> I have a lot of code which could be depending on such things.  I'd like
>> to rewrite such code, but I have no idea how to find such code.  I
>> suppose a loop such as
>>
>>    while ($a["key"]) { ... }

foreach
for
do ... while

finding all those cases you can use grep (generates a lot of false 
positives too):
egrep "(while|for)" -r /path/to/your/scripts

This will not take care of cases where someone has assumed they get an 
array and then they just need to get something from the array, for example:

$var = function_returning_array();
echo $var[0];

>> could be depending on $a sometimes being null or even being undefined
>> (because ``$a === null'' when $a is undefined).  (These are very weird
>> things about PHP to my way of looking at things.)

I would suggest you to use is_array() function, in case you have 
something else than just null that you get, say you would just send an 
integer.



>> How would you search for something like that?  I just have no idea.  My
>> best idea is to just watch a log of errors and notices and try to spot
>> the code that way.

There ain't a good way to really find that, unitests had been useful, 
just run those and see which ones fail, and then fix the functions tested.

> I guess nobody had much to say here.  I'm tackling easier problems at
> first, but I will eventually have to come back to this and make some
> decision.  I suppose upgrading to PHP 7.4 and watching the a noisy log
> with error_reporting = E_ALL (say) might give me more information.  But,
> of course, I have no way of running every line of code, so there is
> really no obvious strategy for something like that.

You could take the time and write unitests now, it will make things 
easier next time. One such framework for php is https://phpunit.de

As you will write tests for each function now, you will locate all the 
uses of arrays.


-- 

  //Aho

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

php 7.4: null["key"] Meredith Montgomery <mmontgomery@levado.to> - 2022-05-20 17:34 -0300
  Re: php 7.4: null["key"] Meredith Montgomery <mmontgomery@levado.to> - 2022-05-29 23:13 -0300
    Re: php 7.4: null["key"] "J.O. Aho" <user@example.net> - 2022-05-30 08:06 +0200
      Re: php 7.4: null["key"] Meredith Montgomery <mmontgomery@levado.to> - 2022-05-31 19:27 -0300
        Re: php 7.4: null["key"] De ongekruisigde <ongekruisigde@news.eternal-september.org> - 2022-05-31 22:39 +0000
          Re: php 7.4: null["key"] Meredith Montgomery <mmontgomery@levado.to> - 2022-06-02 17:26 -0300
            Re: php 7.4: null["key"] Meredith Montgomery <mmontgomery@levado.to> - 2022-06-03 17:09 -0300
              Re: php 7.4: null["key"] De ongekruisigde <ongekruisigde@news.eternal-september.org> - 2022-06-04 14:35 +0000

csiph-web