PHP8 (Migrating existing PHP7 project to PHP8)
PHP8 (Migrating existing PHP7 project to PHP8)
The latest PHP version is released on November 26, 2020. This version comes with a lot of new features and improvements. In this article, I will implement some of these new features in an existing PHP project.
Project Link
You can download the code source from this github link
Implemented PHP8 features
RFC
PHP8 match expression,
Php8 comes with a new expression match
that replaces switch
in the older versions.
match
expression has a lot of advantages compared to traditional PHP switch cases
.
Some of those advantages are
- match expression offer shorter code block
- it does not require
break
- there is no type coercion
Code implementation
Here, in this code, the ship
parameter is passed through the createForm
The ship
object type is instantiated depending on this parameter.
- Older PHP version
public function createFromData(array $data): ?AbstractShip
{
switch ($data['team']) {
case 'empire':
$ship = (new Ship())
->setJediFactor($data['jedi_factor']);
break;
case 'rebel':
$ship = new RebelShip();
break;
case 'broken':
$ship = new BrokenShip();
break;
case 'bounty hunter':
$ship = new BountyHunterShip();
break;
default:
$ship = (new Ship())
->setJediFactor($data['jedi_factor']);
}
}
- What we got with PHP8
match
public function createFromData(array $data): ?AbstractShip
{
if (!isset($data['team'])) {
return null;
}
$ship = match ($data['team']) {
'rebel' => new RebelShip(),
'broken' => new BrokenShip(),
'bounty hunter' => new BountyHunterShip(),
default => (new Ship())->setJediFactor($data['jedi_factor'])
};
}
RFC
Constructor properties promotion, see
As in Javascript, PHP8 introduces the use of syntactic sugar
to define class constructor. Below is the
new implementation.
Code implementation
- Before
private $jsonFixturesLoader;
public function __construct(LoaderInterface $jsonFixturesLoader)
{
$this->jsonFixturesLoader = $jsonFixturesLoader;
}
- After
public function __construct(
private LoaderInterface $jsonFixturesLoader
){}
Named arguments
It is now totally possible to name an argument passed to a function or method call.
Code implementation
- Before
$battleResult = $battleManager->battle(
$ship1,
$ship1Quantity,
$ship2,
$ship2Quantity,
$battleType
);
- After
$battleResult = $battleManager->battle(
ship1: $ship1,
ship1Quantity: $ship1Quantity,
ship2: $ship2,
ship2Quantity: $ship2Quantity,
battleType: $battleType
);
Non-capturing catches
In PHP8, whenever you wanted to catch an exception, you can omit the catch exception variable
Code Implementation
- Before
try {
$jsonContents = \Safe\file_get_contents($this->filename);
return \Safe\json_decode($jsonContents, true);
} catch (FilesystemException $e) {
throw new ReadFileException(\Safe\sprintf(
'Impossible to read this file %s',
$e->getMessage()
));
}
- After
try {
$jsonContents = \Safe\file_get_contents($this->filename);
return \Safe\json_decode($jsonContents, true);
} catch (FilesystemException ) {
throw new ReadFileException(\Safe\sprintf('Impossible to read this file %s', 'er'));
}
Here the $e
variable is omitted.