Questions tagged [php-8]

For questions specific to the usage and new features of PHP 8. When using this tag, also include the more generic [php] 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 more 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 for the @ operator to silence fatal errors
  • "Leading numeric" strings (i.e. 345 == "345abc" now evaluates to false)
  • The use of 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.

176 questions
59
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
  • 9,348
  • 16
  • 56
  • 117
41
votes
7 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 normal } With that kind of code, I end up with the variable $e that is created for nothing (lots of resources), and PHP_MD…
Matthieu Napoli
  • 42,736
  • 37
  • 154
  • 239
32
votes
5 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
  • 34,956
  • 68
  • 168
  • 266
29
votes
17 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
  • 125,999
  • 87
  • 277
  • 404
19
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
  • 11,780
  • 2
  • 20
  • 35
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
  • 7,468
  • 9
  • 35
  • 47
14
votes
3 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
  • 806
  • 11
  • 25
13
votes
2 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
  • 4,375
  • 5
  • 19
  • 33
10
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
  • 2,758
  • 1
  • 12
  • 46
6
votes
0 answers

Having trouble passing named arguments to internal functions with variadic parameters

I played a bit with php's array_map function and thought about using it with named arguments, as the parameter order always irritates me. (The functions don't do something useful. Just needed something to test.) So far so good, this is working as…
kuh-chan
  • 1,157
  • 9
  • 16
6
votes
2 answers

What is PHP8 JIT compiler

What is PHP8 JIT? and what are the advantages it can bring to the PHP world? What I understand is, it is used for the performance improvement.
San
  • 628
  • 7
  • 23
5
votes
2 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
  • 35,483
  • 13
  • 81
  • 108
4
votes
3 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
  • 65,743
  • 7
  • 83
  • 127
4
votes
1 answer

PHP 8 in Visual Studio Code

I use the latest Version of Visual Studio Code (1.52.1) with PHP Intelephense 1.5.4 on Ubuntu. Despite it is the latest Version it seems not to know the new PHP 8 Syntax. For example it shows an error when using the nullsafe…
Peter
  • 1,499
  • 1
  • 10
  • 15
1
2 3
11 12