My favorite PHP 8.6 feature might be clamp(). Every time I write a nested min(max(...)), I have to stop and think about which limit goes where.
Before I'd upgrade a real application, though, I'd run yesterday's CSV through it, come back from an external login, and make sure a file that says it finished writing actually did. Those are good places for a routine upgrade to surprise you.
The feature and compatibility details below are based on the PHP 8.6 migration guide. Check the official downloads for the current stable build before a production upgrade.
What's useful in PHP 8.6?
Partial application lets you leave an argument open
Say we want the lines that contain ERROR. An arrow function works. In PHP 8.6, a question mark can stand for the argument that will arrive later. The expression creates a Closure. The real function runs when the remaining argument is passed.
// PHP 8.5 and earlier
$containsError = fn (string $line): bool => str_contains($line, 'ERROR');
// PHP 8.6 partial application
$containsError = str_contains(?, 'ERROR');
$errors = array_values(array_filter($lines, $containsError));
This is handy if you write a lot of callbacks for array functions or Laravel collections. If your package still supports PHP 8.5, keep the first form in files older runtimes might load. The older parser can't read the new syntax, even inside an if (PHP_VERSION_ID >= 80600) branch in the same file. See the PHP 8.6 new features.
clamp() puts the two limits where I can see them
A page size should be between 1 and 100. clamp(150, min: 1, max: 100) returns 100. Validate that the value is an integer first. Reversed bounds throw a ValueError, so test configuration that supplies both limits.
Search for any unconditional global clamp() function in your own code or polyfills. The name can collide with PHP's built-in function even if you never call the new one. See the clamp RFC.
Duration and the Polling API have clear audiences
\Time\Duration::fromMilliseconds(250) tells you what unit is in use. That's helpful when you're passing timeouts, backoff delays, or measurements between components.
The Polling API helps event loops monitor many sockets. It's most directly useful to event-loop library authors and people who build long-running processes with many connections. Most Laravel applications will use it through a library, if they need it at all. See the PHP 8.6 feature overview.
What I'd test before switching production
Login and payment flows that return from another site
PHP's native sessions enable session.use_strict_mode and session.cookie_httponly by default, and set session.cookie_samesite to Lax. A cross-site top-level POST from a login or payment provider can return without the session cookie that your application expects.
Check the actual Set-Cookie header and test the whole round trip. Laravel and other frameworks can issue their own cookies. A legacy page using session_start() may use different settings in the same application. If you have a custom save handler, check whether it provides create_sid() and validateId(). See the session defaults RFC and the PHP 8.6 deprecations.
CSV files with commas, quotes, and newlines inside cells
PHP 8.6 deprecates SplFileObject::fgetcsv(), fputcsv(), setCsvControl(), and getCsvControl(). Here fputcsv() means the method on SplFileObject. The procedural functions fgetcsv() and fputcsv() remain available. PHP 8.4's deprecation for relying on the default escape argument is a separate change.
When replacing the methods, set the separator, enclosure, and escape explicitly so the parser matches the files you already accept. Run the same fixtures on PHP 8.5 and 8.6. Include a quoted comma, a multiline field, empty columns, and a final blank line. Compare row counts and cell values, not just whether the import finished. See the deprecation list and fgetcsv documentation.
File output all the way through close
fclose(), file_put_contents(), and copy() can now return false if an internal flush or close fails. Check where the nightly job logs “export complete.” For file_put_contents(), use === false for failure. Zero bytes can be the valid result of writing an empty file.
The defaults for trim(), ltrim(), and rtrim() now remove form feed characters (\f) too. If an old report uses them as separators, test the step that trims the input. See the backward-incompatible changes.
The order I'd use for a real upgrade
- Start with the existing lockfile and change PHP in a test environment. It helps to isolate the runtime change before you update Laravel or Composer dependencies.
- Check CLI, the web worker, and the queue workers independently. Each can run a different binary, load a different configuration, or have different extensions.
- Run the session, CSV, and file-output cases with
E_ALLenabled. Make sure you have a rollback image and a way to rerun the affected jobs safely. - If you're skipping versions, read the migration notes for each step you skipped.
php -v
php --ini
composer why-not php 8.6.0
composer check-platform-reqs --lock
Composer's why-not command can show which dependencies declare a conflict with that target version. Run check-platform-reqs --lock in the PHP 8.6 environment to compare its actual PHP and extensions with the lockfile.
A note on the road to PHP 9
A case-insensitive constant declaration was already dropped in PHP 8.0. An old call such as define('APP_ENV', 'production', false) still passes a third argument, which is deprecated in 8.6. That call can drop the third argument.
The proposal to turn reads of undefined variables into errors targets PHP 9.0. You can clean up the warnings you already see while you prepare for that transition. The PHP 9 migration article is available in Japanese.
If I had time for one thing today, I'd save a representative CSV fixture and write down what should come out of it. That'll still help on the next upgrade.
Q&A
What should I test first when moving to PHP 8.6?
Change PHP in a test environment while holding application dependencies steady. Start with external login and payment returns, CSV import and export, and successful and failed file writes.
What should I check after an external login or payment redirect?
Test the return request from the real provider, especially if it returns with a POST. Check whether the session cookie arrives and which code issues that cookie. Your framework and native PHP sessions may use different settings.
What should I put in a CSV migration fixture?
Use a normal production-shaped sample and add quoted commas, a quoted multiline value, empty fields, and a trailing blank line. On both versions, compare the row and column counts and confirm that the cells have the same values.


コメントを残す