PHP 8.5 for busy developers

Five additions of PHP 8.5 change everyday code: the pipe operator, two array edge helpers, an attribute for ignored return values, an object model for URIs, and a clone that takes overrides. Every program below runs on 8.5 and prints what the page shows.

The |> pipe operator

Nested calls read inside out. The pipe sends each result into the next function, left to right, with no temp variables. Keep stages short - one expression each, either a first-class callable or an arrow function.

<?php $title = " Hello PHP World "; $slug = $title |> trim(...) |> (fn($s) => str_replace(" ", "-", $s)) |> strtolower(...); echo $slug, PHP_EOL;

php pipe.php

hello-php-world

The first stage trims the padding, the arrow function replaces each space, and the last stage lowercases the result. The same work as three nested calls, read in the order it happens.

array_first and array_last

Edge reads used to need a ternary with array_key_first(). array_first() and array_last() return the value directly, and null for an empty array - composing neatly with ??.

<?php class Event { public function __construct(public string $name) { } } $events = [new Event("login"), new Event("logout")]; $first = array_first($events) ?? new Event("none"); $last = array_last($events); echo $first->name, PHP_EOL, $last->name, PHP_EOL; var_dump(array_first([]));

php arrays.php

login
logout
NULL

The first read pads the empty case with a default object, the last read takes the final element, and the empty array proves the null that makes ?? work.

#[NoDiscard]

Dropping a return value by accident is silent: the call succeeds and the result evaporates. Mark the function and PHP warns when the value is ignored. A deliberate void cast marks the value as intentionally unused.

<?php #[NoDiscard] function price(float $base): float { return $base * 1.2; } set_error_handler(function (int $errno, string $message): bool { echo "Warning: ", $message, PHP_EOL; return true; }); price(10.0); $total = price(10.0); echo "used: ", $total, PHP_EOL;

php nodiscard.php

Warning: The return value of function price() should either be used or intentionally ignored by casting it as (void)
used: 12

The first call throws the price away and PHP warns; the second call keeps the value, so nothing is printed. The custom error handler is only there to keep the file name and line out of the output; without it the same warning goes to the error log with its position.

The URI extension

Hand-rolled URL checks drift into bugs. The URI extension parses and normalizes addresses as objects instead of string surgery, following RFC 3986 and the WHATWG URL standard.

<?php $uri = new Uri\Rfc3986\Uri("https://php.net/releases/8.5/en.php?tab=notes#top"); echo $uri->getHost(), PHP_EOL; echo $uri->getPath(), PHP_EOL; echo $uri->getQuery(), PHP_EOL;

php uri.php

php.net
/releases/8.5/en.php
tab=notes

Each part has its own getter, so no regular expression has to guess where the host ends. The Uri\WhatWg\Url class follows the browser standard for the same job, and both classes refuse an address they cannot parse instead of returning half of it.

Clone with overrides

Override-style copies used to rebuild objects by hand. Now clone() takes new values for chosen properties. Full details: What is new in PHP 8.5.

<?php class Color { public function __construct( public int $red, public int $green, public int $blue, public int $alpha = 255, ) {} public function withAlpha(int $alpha): self { return clone($this, ["alpha" => $alpha]); } } $base = new Color(255, 0, 0); $faded = $base->withAlpha(128); echo $base->alpha, PHP_EOL, $faded->alpha, PHP_EOL; echo $base === $faded ? "same" : "different", PHP_EOL;

php clone.php

255
128
different

The copy carries every property of the original except the one in the override list, and the original keeps its own value. __clone() still runs after the overrides are applied, so a deep copy of a child object stays possible.

Exercises

  1. Add a fourth stage to the pipe that cuts the slug to 20 characters and print the result.
  2. Call array_last() on an empty array and dump what it returns.
  3. Mark a method with #[NoDiscard] and ignore its result once with and once without the (void) cast.
  4. Print the scheme and the port of a URI that names both.
  5. Give Color a second override method for red and prove that the original object did not change.

Questions

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

  1. What does the |> operator do with the value on its left?

    Answer

    It passes the value as the argument of the callable on its right

    The pipe is a call whose argument is the left value, so the stages read in the order they run.

  2. What does trim(...) mean in a pipe stage?

    Answer

    A first-class callable built from the function name

    The three dots turn a function name into a Closure, which the pipe can call.

  3. What does array_first([]) return?

    Answer

    null

    The helper returns null for an empty array, which is what makes ?? a safe default.

  4. Which attribute makes PHP warn when a return value is ignored?

    Answer

    #[NoDiscard]

    The attribute marks the return value as one the caller should keep or drop on purpose.

  5. What does this program print? <?php $title = " Hello PHP World "; $slug = $title |> trim(...) |> (fn($s) => str_replace(" ", "-", $s)) |> strtolower(...); echo $slug;

    Answer

    hello-php-world

    Trim drops the padding, the arrow function turns spaces into dashes and strtolower ends the job.

  6. What does this program print? <?php $events = ["login", "logout"]; echo array_first($events), " ", array_last($events);

    Answer

    login logout

    array_first() reads the first value and array_last() the last one of the same array.

  7. What does this program print? <?php #[NoDiscard] function double(float $base): float { return $base * 2; } echo double(10);

    Answer

    20

    The value is used by echo, so the attribute has nothing to warn about and the function returns 20.

  8. What does this program print? <?php $uri = new Uri\Rfc3986\Uri("https://php.net/releases/8.5/en.php?tab=notes"); echo $uri->getHost();

    Answer

    php.net

    getHost() returns the host alone, without the scheme and without the path.

  9. What does this program print? <?php class Color { public function __construct(public int $alpha = 255) { } public function withAlpha(int $alpha): self { return clone($this, ["alpha" => $alpha]); } } $base = new Color(); $faded = $base->withAlpha(128); echo $base->alpha, " ", $faded->alpha;

    Answer

    255 128

    The clone takes the new alpha for the copy only; the original object keeps 255.

  10. What happens when a value returned by a #[NoDiscard] function is dropped without a cast?

    Answer

    PHP prints a warning and the program continues

    The warning is a notice-level message: the program continues, and a (void) cast says the drop was intended.

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