Questions tagged [php-8.1]

For questions specific to the usage and new features of PHP 8.1. Also, include the more generic [php] tag when using this tag.

PHP 8.1 (released November 2021) includes many new features including:

A full list of changes can be found in the migration guide.

336 questions
73
votes
5 answers

Constant FILTER_SANITIZE_STRING is deprecated

I have installed PHP 8.1 and I started testing my old project. I have used the filter FILTER_SANITIZE_STRING like so: $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); Now I get this error: Deprecated: Constant…
Dharman
  • 30,962
  • 25
  • 85
  • 135
59
votes
12 answers

Migration to PHP 8.1 - how to fix Deprecated Passing null to parameter error - rename build in functions

PHP 8.1 has deprecated passing null as parameters to a lot of core functions. My main problem is with functions like htmlspecialchars(php) and trim(php), where null no longer is silently converted to the empty string. To fix this issue without going…
Janis T
  • 693
  • 1
  • 3
  • 5
46
votes
8 answers

How to get all values of an enum in PHP?

PHP 8.1 is almost getting released, including support for Enumerations. I was testing some of the enum functionality and couldn't find much documentation about it. Hence my question: how do I get all values of an enum?
Dees Oomens
  • 4,554
  • 3
  • 29
  • 61
35
votes
2 answers

Reference: Return type of ... should either be compatible with ..., or the #[\ReturnTypeWillChange] attribute should be used

In PHP 8.1, the following code, which worked in previous versions: class Example implements Countable { public function count() { return 42; } } Raises a deprecation notice: Deprecated: Return type of Example::count() should either…
IMSoP
  • 89,526
  • 13
  • 117
  • 169
25
votes
4 answers

php 8.1 - explode(): Passing null to parameter #2 ($string) of type string is deprecated

Coming across some deprecated errors with 8.1 I want to address. PHP Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in... //explode uids into an array $comp_uids = explode(',',…
user756659
  • 3,372
  • 13
  • 55
  • 110
21
votes
3 answers

Is there a type hint for an array of objects of a specific class in PHP 8.2?

Is there a type hint in PHP 8.2 which allows using ExampleClass[] for declaring it is an array of objects of the class ExampleClass? In my specific case, ExampleClass is called Task What I want but does not work: private Task[] $tasks; My PHPStorm…
WebDevPassion
  • 422
  • 1
  • 4
  • 12
21
votes
3 answers

PHP Enums `__toString` magic method

I am wondering why it is not possible to provide an __toString implementation for enum. IDE says "Enum may not include '__toString'". However it was the first thing I thought about once I created enum. Previously I used Value Objects encapsulating…
rela589n
  • 817
  • 1
  • 9
  • 19
20
votes
3 answers

How to check if enum type?

How can I tell if a variable is of type enum? I have installed PHP 8.1 on my Ubuntu 20.04. I'm testing the new "enum" types. Is something like that possible? is_enum($var) gettype($var) === 'enum'
Richi RM
  • 829
  • 3
  • 11
18
votes
2 answers

What is never return type in PHP 8.1

PHP 8.1 introduces never return type, what is it? and what is the difference between never and void?
Rain
  • 3,416
  • 3
  • 24
  • 40
13
votes
2 answers

How do I correct syntax highlight in VS Code for PHP 8.1 Enum Class?

My Code: namespace App\Enum; enum HomeStatus: string { case RENT = 'rent'; case MOVE_IN = 'move_in'; case SOLD = 'sold'; case COMING_SOON = 'coming_soon'; } The code above is executing and working but the editor has the following…
Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
10
votes
2 answers

How to iterate over enum

I want to iterate over enum: enum Shapes { case RECTANGLE; case SQUARE; case CIRCLE; case OVAL; } I get Shapes const not defined if I do this: foreach (Shapes as $shape) { } The best solution I came with is to manually create array…
ICE
  • 1,667
  • 2
  • 21
  • 43
9
votes
2 answers

Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated codeigniter

I'm running codeigniter 4.1.5 and php 8.1.0 and when i try to input data this is the error i got Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in…
user14638393
8
votes
1 answer

PHP Deprecated: Automatic conversion of false to array is deprecated adodb-mssqlnative.inc.php on line 154

We are upgrading PHP to version 8.1. Using MS Sql Server DB. It all seems to work correctly but I see repeated messages in the log file: [03-Feb-2022 11:51:18 America/New_York] PHP Deprecated: Automatic conversion of false to array is deprecated…
kuru
  • 77
  • 1
  • 1
  • 4
8
votes
4 answers

PHP is not working after updating to PHP 8.1 RC5

After updating PHP 7.4 to PHP 8.1 RC5, PHP has stopped working on my machine and produces following error: PHP Warning: PHP Startup: ^(text/|application/xhtml+xml) (offset=0): unrecognised compile-time option bit(s) in Unknown on line 0 it…
user335870
  • 588
  • 2
  • 11
  • 22
7
votes
3 answers

Dealing with PHP 8.1 warning for undefined array key

We are upgrading to PHP 8.1. A new feature is that an undefined array key throws a warning. Unfortunately this interferes with the ability to easily use associative arrays such as $_SESSION variables. I understand the virtues of predefining…
Jeffrey Simon
  • 918
  • 3
  • 11
  • 25
1
2 3
22 23