Take the State of PHP 2026 Survey →

Ensure correct signatures of magic methods View as Markdown

Implemented (PHP 8.0)
Authors
Gabriel Caruso (carusogabriel@php.net)
Date
2020-04-05
Version
1.0
Target
PHP 8.0
Implementation
https://github.com/php/php-src/pull/4177
Source
wiki.php.net/rfc/magic-methods-signature

Vote

Ensure correct signatures of magic methods closed
Yes
45
No
2
47 voters
alcaeus Yes 2020-06-04 15:44
asgrim Yes 2020-05-29 21:46
ashnazg Yes 2020-06-02 15:18
beberlei Yes 2020-06-13 19:28
bmajdak Yes 2020-05-29 17:43
brzuchal Yes 2020-06-05 12:18
bwoebi Yes 2020-05-29 17:11
carusogabriel Yes 2020-05-29 16:45
colinodell Yes 2020-06-10 10:41
dams Yes 2020-06-03 15:35
danack Yes 2020-05-30 10:07
daverandom Yes 2020-06-02 12:13
duncan3dc Yes 2020-05-30 15:29
duodraco Yes 2020-06-03 18:14
galvao Yes 2020-05-30 21:00
geekcom Yes 2020-06-16 00:40
girgias Yes 2020-05-29 22:35
heiglandreas Yes 2020-06-13 14:57
jasny Yes 2020-06-02 22:57
jbnahan Yes 2020-06-02 09:37
jhdxr No 2020-06-01 01:31
kalle Yes 2020-05-29 16:50
kguest Yes 2020-06-03 10:04
klaussilveira Yes 2020-06-16 18:25
kocsismate Yes 2020-05-29 17:21
marandall Yes 2020-05-29 19:04
marcio Yes 2020-05-29 17:14
mariano Yes 2020-05-29 16:54
mcmic Yes 2020-06-03 09:48
mike Yes 2020-06-08 11:39
narf Yes 2020-06-19 11:23
ocramius Yes 2020-05-29 17:07
patrickallaert Yes 2020-06-11 10:04
pierrick Yes 2020-06-15 14:02
ramsey Yes 2020-05-29 23:34
ruudboon Yes 2020-06-03 07:12
santiagolizardo Yes 2020-06-15 17:40
sergey Yes 2020-06-01 13:00
sirsnyder Yes 2020-06-15 22:42
stas No 2020-05-29 18:01
svpernova09 Yes 2020-05-29 16:46
tandre Yes 2020-06-19 13:45
trowski Yes 2020-05-29 17:43
wyrihaximus Yes 2020-06-02 21:16
yunosh Yes 2020-06-04 08:20
zeev Yes 2020-06-03 11:56
zimt Yes 2020-06-03 13:37

Discussions on the mailing list

[RFC] [DISCUSSION] Ensure correct magic methods' signatures when typed
by Gabriel Caruso · 23 messages
[VOTE] Ensure correct signatures of magic methods
by Gabriel Caruso · 10 messages
Drop warning about non-public magic methods
by Gabriel Caruso · 5 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

It is currently possible to write magic methods that have signatures that don't match the signature expected, such as __clone(): float or __isset(): Closure.

This behavior of allowing incorrect signatures was reported as a bug.

As ensuring magic methods have correct signatures is a backward compatible break for code that currently has incorrect signatures, this change would only be appropriate in a major PHP release.

Motivation

PHP's Magic Methods is something that PHP provides allowing developers to track and act on specific changes of behavior of a certain class. Given that fact, the same should ensure that the end-users are using these methods consistently across different codebases.

Since the introduction of types in PHP 7.0, only the checks list below were introduced to make sure that developers are using PHP's magic methods correctly:

For PHP 8, this RFC aims to expand these checks.

Proposal

This RFC proposes to add parameter and return types checks per the following details. Other magic methods will be not modified.

The proposal follows the Magic Methods documentation.

Foo::__call(string $name, array $arguments): mixed;
 
Foo::__callStatic(string $name, array $arguments): mixed;
 
Foo::__clone(): void;
 
Foo::__debugInfo(): ?array;
 
Foo::__get(string $name): mixed;
 
Foo::__invoke(mixed $arguments): mixed;
 
Foo::__isset(string $name): bool;
 
Foo::__serialize(): array;
 
Foo::__set(string $name, mixed $value): void;
 
Foo::__set_state(array $properties): object;
 
Foo::__sleep(): array;
 
Foo::__unserialize(array $data): void;
 
Foo::__unset(string $name): void;
 
Foo::__wakeup(): void;

Note: The __construct() and __destruct() methods won't suffer any changes. They won't allow void as a return type given the fact that (almost) all languages, including PHP, don't have the concept of Constructors and Destructors “returning” something after their execution.

Backward Incompatible Changes

To Magic Methods without types declared

None.

To Magic Methods with the wrong signatures

Magic methods' signatures not matching the ones listed above, an error will be thrown, a Fatal Error more specific, following the errors of the same kind that are placed today.

RFC Impact

Scraping the top 1000 Composer packages (using Nikita's script), the results show only 7 occurrences of not matching signatures.

Luckily, none of them is a problem as __call(), __callStatic() and __get() do not have checks at this time.

Even with a mixed RFC that wouldn't be a problem, as a specific type can override it, by the Liskov Substitution Principle.

Future Scope

This RFC only aims to add checks for the methods' signatures but as a Future Scope, a runtime check of what is been returning in the methods could be added, same as

Voting

Voting started on 2020-05-29 at 18h (CEST) and ends on 2020-06-19 at 18h (CEST).

Vote: Ensure correct signatures of magic methods — Yes: 45 · No: 2 (closed)

External resources

- Discussion thread: https://externals.io/message/109542