Skip to content

Conversation

@IonBazan
Copy link
Contributor

@IonBazan IonBazan commented Sep 22, 2025

PHP 8.5 deprecates using null as array key (accessing, setting, as well as things like array_key_exists()):

Using null as an array offset or when calling array_key_exists() is now deprecated. Instead an empty string should be used. RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_using_values_null_as_an_array_offset_and_when_calling_array_key_exists

I have personally encountered this deprecation in parseVerbosity() as usually it's set to null when calling $this->line('message'). Other places were discovered by running the test suite. Please note that most likely not all occurrences were found yet.

$keys = is_array($key) ? $key : func_get_args();

return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
return array_all($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to consider null to be ''? If we have an array containing an item with '', it will be returned, when null is given, which can be unintentional.

Copy link
Contributor Author

@IonBazan IonBazan Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using null as key currently has the same behaviour (PHP 8.4):

> $arr[null] = 'test'
= "test"

> $arr
= [
    "" => "test",
  ]

> $arr['']
= "test"

When it comes to Collection and Arr, I'm a bit on the fence. We can either silently cast it to string for the users to keep things working as before, or let the deprecation be triggered and expect an userland logic change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation 👍🏻

@donnysim
Copy link
Contributor

donnysim commented Sep 22, 2025

I mean, TKey is defined as TKey of array-key which mean only int|string. If you pass a null value to that is your bug and should be fixed in your code. Arrays cannot have null as keys so what are we "fixing" here? If there are bugs related to it in the framework, it should preferably be fixed on usage, not on implementation.

@IonBazan
Copy link
Contributor Author

@donnysim I agree, however this test suggests we could still use null:

#[DataProvider('collectionClassProvider')]
public function testGetWithNullReturnsNull($collection)
{
$data = new $collection([1, 2, 3]);
$this->assertNull($data->get(null));
}

I could imagine many users having $collection->get($another->pluck('id')->first()) or similar which will require adding ?? '' in many places to prevent deprecations from occurring which makes it less "fluent".

I also noticed that getOrPut might have inconsistent behaviour when null is passed - depending whether an empty string key exists or not. That's because offsetSet contains a null check. Let me add a test to demonstrate it.

@taylorotwell taylorotwell merged commit e8ed95d into laravel:12.x Sep 23, 2025
62 of 63 checks passed
@IonBazan IonBazan deleted the null-array-key branch September 24, 2025 05:49
tegos pushed a commit to tegos/laravel-framework that referenced this pull request Sep 28, 2025
* Fix PHP 8.5 null-key deprecations

* Fix getOrPut

* Update InteractsWithIO.php

* Update Collection.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants