rfc:magic-methods-signature

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:magic-methods-signature [2020/04/15 23:18] – Grammar carusogabrielrfc:magic-methods-signature [2020/04/28 15:08] carusogabriel
Line 15: Line 15:
 Nowadays, PHP already checks the signature for the following methods: Nowadays, PHP already checks the signature for the following methods:
  
-  * clone return type: https://3v4l.org/Ub54p +  * //%%__%%clone// return type: https://3v4l.org/Ub54p 
-  * construct return type: https://3v4l.org/CCL11 +  * //%%__%%construct// return type: https://3v4l.org/CCL11 
-  * destruct return type: https://3v4l.org/HNkgW+  * //%%__%%destruct// return type: https://3v4l.org/HNkgW 
 +  * //%%__%%toString// return type: https://3v4l.org/jIg7b/rfc#git-php-master
  
 ===== Motivation ===== ===== Motivation =====
  
-Since the introduction of types in PHP 7.0only the 3 checks listed above were introduced to make sure that developers are using PHP's magic methods correctly.+PHP's Magic Methods is something that PHP provides allowing developers to track and act on specific changes of behavior of a certain classGiven that fact, the same should ensure that the end-users are using these methods consistently across different codebases.
  
-For PHP 8, this RFC aims to expand these checks, making their usage standard across different projects and applications.+Since the introduction of types in PHP 7.0, only the checks listed above (under "Introduction") 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 ===== ===== Proposal =====
Line 41: Line 44:
  
 Foo::__clone(): void; Foo::__clone(): void;
 +
 +Foo::__debugInfo(): ?array;
  
 /** @return mixed */ /** @return mixed */
Line 46: Line 51:
  
 Foo::__isset(string $name): bool; Foo::__isset(string $name): bool;
 +
 +Foo::__serialize(): array;
  
 /** @param mixed $value */ /** @param mixed $value */
 Foo::__set(string $name, $value): void; Foo::__set(string $name, $value): void;
 +
 +/** @return mixed */
 +Foo::__set_state(array $properties);
 +
 +Foo::__sleep(): array;
 +
 +Foo::__unserialize(array $data): void;
  
 Foo::__unset(string $name): void; Foo::__unset(string $name): void;
 +
 +Foo::__wakeup(): void;
 </code> </code>
  
-**Note:** The //%%__%%construct// and //%%__%%destruct// methods won't suffer any change. They won't allow //void// as a return type given the fact that (almost) no language, including PHP, has the concept of Constructors and Destructors "returning" something after their execution. +**Note:** The //%%__%%construct// and //%%__%%destruct// methods won't suffer any change. They won't allow //void// as a return type given the fact that (almost) all languages, including PHP, have the concept of Constructors and Destructors "returning" something after their execution. 
      
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
- 
-An important note here is that if one of the listed magic methods is directly called, and it has the wrong signatures, the error will be thrown regardless. 
- 
-For example https://3v4l.org/CuTNm, after this RFC, the errors about incorrect signatures will be thrown. 
  
 ==== To Magic Methods without types declared ==== ==== To Magic Methods without types declared ====
Line 71: Line 83:
 ===== RFC Impact ===== ===== RFC Impact =====
  
-Scraping the top 1000 Composer packages (using Nikita's [[https://gist.github.com/nikic/a2bfa3e2f604f66115c3e4b8963a6c72|script]]), [[https://gist.github.com/carusogabriel/e0b36e7cd9e6846e04f79008cb7e35d6|the results]] show only 7 occurrences. +Scraping the top 1000 Composer packages (using Nikita's [[https://gist.github.com/nikic/a2bfa3e2f604f66115c3e4b8963a6c72|script]]), [[https://gist.github.com/carusogabriel/e0b36e7cd9e6846e04f79008cb7e35d6|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.  Luckily, none of them is a problem as //%%__%%call//, //%%__%%callStatic// and //%%__%%get// do not have checks at this time. 
Line 79: Line 91:
 ===== Future Scope ===== ===== 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 //%%__%%serialize// (https://3v4l.org/HLiTj) and //%%__%%toString// (https://3v4l.org/Dbe6G).+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 
 + 
 +  * //%%__%%serialize//https://3v4l.org/HLiTj 
 +  * //%%__%%toString//https://3v4l.org/Dbe6G 
 +  * //%%__%%debugInfo//: https://3v4l.org/0EEPh 
 +  * //%%__%%sleep//: https://3v4l.org/dH96A
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
 Yes/No. Yes/No.
 +
 +===== External resources =====
 +
 +- Discussion thread: https://externals.io/message/109542
 +
rfc/magic-methods-signature.txt · Last modified: 2020/08/01 23:34 by carusogabriel