rfc:callableconstructors

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
Last revisionBoth sides next revision
rfc:callableconstructors [2016/02/25 13:23] – Fix formatting salatherfc:callableconstructors [2016/02/25 22:19] – DokuWiki tags danack
Line 1: Line 1:
 ====== PHP RFC: Callable Constructors ====== ====== PHP RFC: Callable Constructors ======
-  * Version: 0.9+  * Version: 1.0
   * Date: 2016-02-25   * Date: 2016-02-25
   * Author: Dan Ackroyd, Danack@php.net   * Author: Dan Ackroyd, Danack@php.net
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: https://wiki.php.net/rfc/callableconstructors   * First Published at: https://wiki.php.net/rfc/callableconstructors
  
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, rather than being called with $fn() syntax, then the function is erroneously called through the 'instance' scope, rather than through static scope. 
 + 
 + 
 +<code php> 
 + 
 +class A { 
 +    public function __construct() { 
 +        return "Why would you even do this."; 
 +    } 
 +     
 +    public function foo() { 
 +        $fn = [self::class, '__construct']; 
 +        // Calling directly as a callable like: 
 +        // $result = $fn(); 
 +        // Correctly fails with the error message: 
 +        // Error: Non-static method A::__construct() cannot be called statically 
 +         
 +        // However calling with 'call user function', is erroneously allowed. 
 +        $result = call_user_func($fn); 
 +        var_dump($result); 
 +    } 
 +
 + 
 +$a = new A; 
 +$a->foo(); 
 + 
 + 
 +// Current output is:  
 +// string(27) "Why would you even do this." 
 + 
 +// Output after RFC would be: 
 +// object(A)#1 (0) { 
 +// } 
 +</code> 
 + 
 + 
 +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, '__construct']; 
 +//This calls the constructor as an instance method, without creating a new object, in exactly the same 
 +// way as `$foo->__contruct();` calls the method without creating an object. 
 +</code>
  
 ===== Proposed PHP Version ===== ===== Proposed PHP Version =====
rfc/callableconstructors.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1