rfc:magic-methods-signature

PHP RFC: Ensure correct signatures of magic methods

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).

Ensure correct signatures of magic methods
Real name Yes No
alcaeus (alcaeus)  
asgrim (asgrim)  
ashnazg (ashnazg)  
beberlei (beberlei)  
bmajdak (bmajdak)  
brzuchal (brzuchal)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
colinodell (colinodell)  
dams (dams)  
danack (danack)  
daverandom (daverandom)  
duncan3dc (duncan3dc)  
duodraco (duodraco)  
galvao (galvao)  
geekcom (geekcom)  
girgias (girgias)  
heiglandreas (heiglandreas)  
jasny (jasny)  
jbnahan (jbnahan)  
jhdxr (jhdxr)  
kalle (kalle)  
kguest (kguest)  
klaussilveira (klaussilveira)  
kocsismate (kocsismate)  
marandall (marandall)  
marcio (marcio)  
mariano (mariano)  
mcmic (mcmic)  
mike (mike)  
narf (narf)  
ocramius (ocramius)  
patrickallaert (patrickallaert)  
pierrick (pierrick)  
ramsey (ramsey)  
ruudboon (ruudboon)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
sirsnyder (sirsnyder)  
stas (stas)  
svpernova09 (svpernova09)  
tandre (tandre)  
trowski (trowski)  
wyrihaximus (wyrihaximus)  
yunosh (yunosh)  
zeev (zeev)  
zimt (zimt)  
Final result: 45 2
This poll has been closed.

External resources

rfc/magic-methods-signature.txt · Last modified: 2020/08/01 23:34 by carusogabriel