rfc:callableconstructors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:callableconstructors [2016/02/25 13:23] – Fix formatting salathe | rfc:callableconstructors [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Callable Constructors ====== | ====== PHP RFC: Callable Constructors ====== | ||
- | * Version: | + | * Version: |
* Date: 2016-02-25 | * Date: 2016-02-25 | ||
* Author: Dan Ackroyd, Danack@php.net | * Author: Dan Ackroyd, Danack@php.net | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
Line 139: | Line 139: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | None known | + | |
+ | There is a single BC break known. When the %%__%%construct method is called statically from within the scope of the containing class, and the call is made with call_user_function, | ||
+ | |||
+ | |||
+ | <code php> | ||
+ | |||
+ | class A { | ||
+ | public function __construct() { | ||
+ | return "Why would you even do this."; | ||
+ | } | ||
+ | |||
+ | public function foo() { | ||
+ | $fn = [self:: | ||
+ | // Calling directly as a callable like: | ||
+ | // $result = $fn(); | ||
+ | // Correctly fails with the error message: | ||
+ | // Error: Non-static method A:: | ||
+ | |||
+ | // However calling with 'call user function', | ||
+ | $result = call_user_func($fn); | ||
+ | var_dump($result); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $a = new A; | ||
+ | $a-> | ||
+ | |||
+ | |||
+ | // Current output is: | ||
+ | // string(27) "Why would you even do this." | ||
+ | |||
+ | // Output after RFC would be: | ||
+ | // object(A)#1 (0) { | ||
+ | // } | ||
+ | </ | ||
+ | |||
+ | |||
+ | The code would need to be changed to call the %%__%%construct method through the instance to continue to have the code behave as before. i.e. | ||
+ | <code php> | ||
+ | $fn = [$this, ' | ||
+ | //This calls the constructor as an instance method, without creating a new object, in exactly the same | ||
+ | // way as `$foo-> | ||
+ | </ | ||
===== Proposed PHP Version ===== | ===== Proposed PHP Version ===== |
rfc/callableconstructors.1456406625.txt.gz · Last modified: 2017/09/22 13:28 (external edit)