Modern PHP tour: 5 to 8.4

Modern PHP is the sum of three big steps: objects in PHP 5, types and speed in PHP 7, and expression-shaped code in PHP 8. The lessons before this one used all three, and this page puts them on a timeline.

PHP 5 OOP

PHP 5 turned PHP into an object language: classes, visibility, exceptions. Most legacy code you will meet was born here. Start the timeline at PHP 5 - the hub for the whole story is PHP versions.

<?php class Post { public string $title; public function __construct(string $title) { $this->title = $title; } } $post = new Post("Hello"); echo $post->title, PHP_EOL;

php php5.php

Hello

A class with a typed property and a constructor is the shape that PHP 5 introduced and that every later version kept. The type on the property arrived later, but the object and its method are the same idea.

PHP 7 speed and types

PHP 7 doubled execution speed and added scalar type hints plus return types. Sloppy numeric code that relied on silent coercion started failing loudly - which was the point. See PHP 7.

<?php function add(int $a, int $b): int { return $a + $b; } echo add(2, 3), PHP_EOL; try { echo add([], 1), PHP_EOL; } catch (TypeError $e) { echo get_class($e), PHP_EOL; }

php php7.php

5
TypeError

Two integers add up, and an array where an integer is declared is refused before the body runs. The type hints of PHP 7 are what turn that mistake into a clear line instead of a strange result.

PHP 8 expression power

PHP 8 made code expression-shaped: match, nullsafe calls, named arguments, attributes. Branches collapsed into values you can assign and return. Tour it via PHP 8 and PHP 8.0.

<?php $status = "paid"; $label = match ($status) { "new" => "Fresh", "paid" => "Done", default => "Unknown", }; echo $label, PHP_EOL;

php php8.php

Done

The match produces the value and the assignment takes it, so no temporary variable and no break are needed. A status that matches no arm takes the default, and without a default the match would throw.

Upgrade path to 8.5

Jump one minor version at a time on a copy of the project: run the test suite, read the migration notes, fix deprecations before they become errors. The linters from the last lesson catch half of this work mechanically.

Exercises

  1. Give Post a second property with a default and print it.
  2. Call add() with a numeric string and describe what happens.
  3. Add a fourth status to the match and an arm for it.
  4. Find one deprecated call in an old project and replace it with the current form.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What did PHP 5 bring to the language?

    Answer

    Classes, visibility and exceptions

    The object model of PHP 5 is the base that every later version extended.

  2. What did PHP 7 add?

    Answer

    Scalar type hints, return types and a large speed gain

    The engine rewrite made the language faster and the signatures could finally name scalar types.

  3. Which PHP 8 feature turns a branch into a value?

    Answer

    The match expression

    A match returns the value of its arm, so it can sit on the right side of an assignment.

  4. How should an upgrade to a newer PHP version be done?

    Answer

    One minor version at a time on a copy of the project

    A step of one minor keeps the list of failures short and the running site safe.

  5. What does this program print? <?php class Post { public string $title; public function __construct(string $title) { $this->title = $title; } } $post = new Post("Hello"); echo $post->title;

    Answer

    Hello

    The constructor stores the argument in the property, and the echo reads it back.

  6. What does this program print? <?php function add(int $a, int $b): int { return $a + $b; } echo add(2, 3);

    Answer

    5

    The two integers are added, not joined as text.

  7. What does this program print? <?php $status = "new"; $label = match ($status) { "new" => "Fresh", "paid" => "Done", default => "Unknown", }; echo $label;

    Answer

    Fresh

    The status matches the first arm, and the match returns the string of that arm.

  8. What does this program print? <?php function add(int $a, int $b): int { return $a + $b; } try { echo add([], 1); } catch (TypeError $e) { echo get_class($e); }

    Answer

    TypeError

    An array cannot be taken for an int, so the call throws a TypeError before the body.

  9. Why is jumping several minor versions at once a bad plan?

    Answer

    All deprecations and removals arrive together, and no single step can be tested

    One step at a time keeps the failures attributable to one upgrade and the tests meaningful.

  10. What do named arguments let a caller do?

    Answer

    Pass a value by its parameter name

    The name selects the parameter, so the order does not matter and optional ones can be skipped.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev