Make WordPress Core

Opened 4 weeks ago

Closed 4 weeks ago

#65637 closed defect (bug) (fixed)

PHP 8.6: Returning a value from a constructor is deprecated

Reported by: swissspidy Owned by: swissspidy
Priority: normal Milestone: 7.1
Component: General Version:
Severity: normal Keywords: has-patch commit php86
Cc: Focuses: php-compatibility

Description

This RFC to deprecated returning from constructors just landed in PHP nightlies: https://php.watch/r/188

Now in the logs I see:

Deprecated: Returning a value from a constructor is deprecated in /path/to/wp-includes/pomo/streams.php on line 317

https://github.com/WordPress/wordpress-develop/blob/e7f523f6c91829cea08b9429c4f73b5ecc455768/src/wp-includes/pomo/streams.php#L317

There may be more instances.

Change History (7)

This ticket was mentioned in Slack in #cli by swissspidy. View the logs.


4 weeks ago

This ticket was mentioned in PR #12538 on WordPress/wordpress-develop by @iamchitti.


4 weeks ago
#3

  • Keywords has-patch added

## Trac ticket

Fixes https://core.trac.wordpress.org/ticket/65637

## Problem

PHP 8.6 introduces a deprecation from the Constructor/Destructor Return Values RFC (see also https://php.watch/r/188). Returning a value from __construct() or __destruct() now emits:

Deprecated: Returning a value from a constructor is deprecated in /path/to/wp-includes/pomo/streams.php on line 317

A constructor's return value is always discarded by PHP — new Foo() evaluates to the object, never to whatever the constructor returned — so these statements were dead code and are safe to remove.

## Changes

  • streams.phpreturn false;return;. The return was an early bail when file_get_contents() fails; the bare return; preserves that control flow exactly. (The skipped $this->_pos = 0; is redundant anyway, as the parent POMO_StringReader::__construct() already sets it to 0.)
  • class-pop3.phpreturn true; removed. It was the constructor's final statement, so nothing is skipped.

No behavioral change: the returned values were never observable by callers.

## Testing instructions

Requires PHP 8.6. Using the official Docker RC image:

# Before this change (from a clean checkout) — two deprecations
docker run --rm -v "$PWD":/wp -w /wp php:8.6-rc-cli php -r '
  define("ABSPATH", "/wp/src/");
  require "src/wp-includes/pomo/streams.php";
  new POMO_CachedFileReader("/nonexistent.mo");
  require "src/wp-includes/class-pop3.php";
  new POP3();
'

## Usage of AI

Claude Code Opus 4.8 used to scan instances throughout the codebase

#4 @swissspidy
4 weeks ago

  • Keywords commit added
  • Milestone Future Release7.1

#5 @adrianduffell
4 weeks ago

This looks like a simple enough fix. Could we land it before the 7.1 beta 2 release next week?

#6 @swissspidy
4 weeks ago

  • Keywords php86 added

#7 @swissspidy
4 weeks ago

  • Owner set to swissspidy
  • Resolutionfixed
  • Status newclosed

In 62765:

Code Modernization: Avoid returning values in constructors.

Returning values from __construct() and __destruct() is a deprecated practice starting with PHP 8.6, as per a recent RFC.

Props iamchitti, Soean.
Fixes #65637.

Note: See TracTickets for help on using tickets.