Questions tagged [php-8]

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

PHP 8 is the current major version of , released on 26 November 2020. It succeeded , which was the last release of PHP 7. There is a migration guide to assist with upgrading and a complete list of changes.

New features

See a high-level overview of new features or a complete list as part of the migration guide.

Removed features

These features were deprecated in previous releases and removed in PHP 8.0

  • Old PHP 4-style constructors (methods with the same name as the class)
  • The ability to call non-static methods statically
  • The track_errors php.ini directive
  • The __autoload() magic function
  • The ability of the @ operator to silence fatal errors
  • "Leading numeric" strings (i.e. 345 == "345abc" now evaluates to false)
  • Use the salt argument to password_hash().
  • The create_function() function
  • The each() function
  • The read_exif_data() function
  • The money_format() function
  • The use of array_key_exists() with objects

A complete list of removals and other backwards-incompatible changes can be found as part of the migration guide.

593 questions
76
votes
6 answers

Is it possible to type hint more than one type?

Can I allow two different types using type hinting? E.g. parameter $requester could be either of User or File: function log (User|File $requester) { }
Chris
  • 13,100
  • 23
  • 79
  • 162
55
votes
6 answers

Is there a way to catch an Exception without having to create a variable?

In PHP, I sometimes catch some exceptions with try/catch : try { ... } catch (Exception $e) { // Nothing, this is a test that an exception is thrown. } With that kind of code, I end up with the variable $e that is created for nothing (lots…
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
45
votes
0 answers

Is it possible to use named function parameters in PHP?

Is it possible in php like in python to have named function parameters? An example use case is: function foo($username = "", $password = "", $timeout = 10) { } I want to override $timeout: foo("", "", 3); Ugly. I would much rather…
Justin
  • 42,716
  • 77
  • 201
  • 296
38
votes
16 answers

Does PHP allow named parameters so that optional arguments can be omitted from function calls?

Is it possible in PHP to specify a named optional parameter when calling a function/method, skipping the ones you don't want to specify (like in python)? Something like: function foo($a, $b = '', $c = '') { // whatever } foo("hello",…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
37
votes
5 answers

Required parameter $xxx follows optional parameter $yyy

Deprecated: Required parameter $xxx follows optional parameter $yyy in... Since upgrading to PHP 8.0 this error is thrown when running code like this: function test_function(int $var1 = 2, int $var2) { return $var1 / $var2; } This has worked…
miken32
  • 42,008
  • 16
  • 111
  • 154
37
votes
4 answers

Laravel app stopped working after upgrading to php 8

after updating my mac to php 8 laravel app stopped working, this is the error I'm getting: Deprecated: Method ReflectionParameter::getClass() is deprecated in /Users/.../Sites/.../vendor/laravel/framework/src/Illuminate/Container/Container.php on…
Pezhvak
  • 9,633
  • 7
  • 29
  • 39
28
votes
2 answers

How can i use php8 attributes instead of annotations in doctrine?

This is what I would like to use: #[ORM\Column(type: "string")] instead of this: /** * @ORM\Column(type="string") */ But I'm getting this error: (error: Class 'Column' is not annotated with 'Attribute' ) Is it because Doctrine does not support…
glm
  • 393
  • 1
  • 3
  • 6
28
votes
4 answers

Is there a "nullsafe operator" in PHP?

Is there any way to write the following statement using some kind of safe navigation operator? echo $data->getMyObject() != null ? $data->getMyObject()->getName() : ''; So that it looks like this: echo $data->getMyObject()?->getName();
Dennis
  • 1,027
  • 1
  • 14
  • 30
18
votes
4 answers

What is the "Add #[Pure] attribute" inspection in PhpStorm checking for?

I've got a very simple FormRequest class in a Laravel project. Two methods, both of which return a simple array that's partially populated by a method, but the IDE treats them differently.
miken32
  • 42,008
  • 16
  • 111
  • 154
17
votes
2 answers

PHP 8, function alias compatibility `getdir()`

While testing if my php script is php-8 compatible, I got stuck on the following code: function getDir($a, $o = 2) { $d = Floor($a / $o); return ($d % 2 === 0); } Prior to php-8, this worked fine, however, on php-8 it throws: Fatal error:…
0stone0
  • 34,288
  • 4
  • 39
  • 64
15
votes
5 answers

Does PHP have "named parameters" so that early arguments can be omitted and later arguments can be written?

In PHP you can call a function with no arguments passed in so long as the arguments have default values like this: function test($t1 ='test1',$t2 ='test2',$t3 ='test3') { echo "$t1, $t2, $t3"; } test(); However, let's just say I want the last…
matthy
  • 8,144
  • 10
  • 38
  • 47
14
votes
3 answers

How to detect if PHP JIT is enabled

What is the simplest way to detect if PHP is compiled with JIT and JIT is enabled from the running script?
mvorisek
  • 3,290
  • 2
  • 18
  • 53
13
votes
6 answers

How to install PHP 8 on XAMPP

I'm trying to update my PHP version to the brand new PHP 8. I have followed simular steps as this tutorial. But now the following error is shown: 12:06:23 [Apache] Error: Apache shutdown unexpectedly. 12:06:23 [Apache] This may be due to a…
Tim567
  • 775
  • 1
  • 5
  • 22
11
votes
4 answers

Reference - Composer error "Your PHP version does not satisfy requirements" after upgrading PHP

After updating PHP from 7.4 to 8.0, I ran composer update on my existing project, and got an error like this: acme/some-package[1.0.0, ..., 1.4.0] requires php ^5.6.4 || ^7.0 -> your php version (8.0.3) does not satisfy that requirement. What…
IMSoP
  • 89,526
  • 13
  • 117
  • 169
1
2 3
39 40