rfc:autovivification_false

This is an old revision of the document!


PHP RFC: Deprecate autovivification on false and/or null

  • Date: 2021-05-25
  • Author: Kamil Tekiela dharman@php.net
  • Status: Under Discussion
  • Target Version: PHP 8.1

Introduction

PHP natively allows for autovivification (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined. However, there is a little oddity that allows creating an array from a false and null value.

// From undefined
$arr[] = 'some value';
$arr['doesNotExist'][] = 2;
// From false
$arr = false;
$arr[] = 2;
// From null
$arr = null;
$arr[] = 2;

This RFC talks only about autovivification from false and null.

Proposal

The proposal is to disallow autovivification from false values and/or null. We will continue to allow autovivification from undefined variable, but in every other case an error message will be shown:

Cannot use a scalar value as an array

In PHP 8.1, appending to a variable of type false will display a deprecation notice.

In PHP 9.0, this will throw a fatal error.

A secondary vote will decide if autovivification should be forbidden from null values too.

Rationale

Historically, many functions return false on error or array on success. In some scenarios, developers might want to assign an additional element to an array, e.g. a total section at the end of array fetched from PDO. PHP does not complain if the value is false. Whether it should be considered a bug or a “feature” is up for debate.

$stmt = $pdo->query("SELECT 1 WHERE 0");
$arr = $stmt->fetch();
// naively try to append
$arr[] = ['new row values'];

Throwing an error in such cases could save developers from common bugs in their code. On the other hand, this feature could also be considered useful by some developers. The rationale here is that the behaviour is inconsistent. Neither true, 0, nor “” can be used in autovivification today, so does it make sense to allow false?

Allowing autovivification on null can be a sign of a buggy code, but it is less of a problem than false. In PHP, null and undefined are very similar and often treated the same way, e.g. isset() and ??.

Backward Incompatible Changes

Every single usage autovivification on false and/or null will throw a deprecation error in PHP 8.1 and throw a fatal error in PHP 9.0.

Vote

Should we deprecate autovivification from false?

Deprecate autovivification from false?
Real name Yes No
alcaeus (alcaeus)  
asgrim (asgrim)  
ashnazg (ashnazg)  
bmajdak (bmajdak)  
brzuchal (brzuchal)  
bwoebi (bwoebi)  
cschneid (cschneid)  
danack (danack)  
derick (derick)  
dharman (dharman)  
dmitry (dmitry)  
ekin (ekin)  
galvao (galvao)  
ilutov (ilutov)  
kalle (kalle)  
kguest (kguest)  
kocsismate (kocsismate)  
levim (levim)  
lufei (lufei)  
marandall (marandall)  
mcmic (mcmic)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
patrickallaert (patrickallaert)  
pierrick (pierrick)  
pollita (pollita)  
ramsey (ramsey)  
reywob (reywob)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
sergey (sergey)  
tandre (tandre)  
theodorejb (theodorejb)  
trowski (trowski)  
wyrihaximus (wyrihaximus)  
Final result: 34 2
This poll has been closed.


Should we deprecate autovivification from null?

Deprecate autovivification from null?
Real name Yes No
Final result: 0 0
This poll has been closed.
rfc/autovivification_false.1622928466.txt.gz · Last modified: 2021/06/05 21:27 by dharman