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 revisionBoth sides next revision
rfc:callableconstructors [2016/02/25 13:23] – Fix formatting salatherfc:callableconstructors [2016/02/25 14:04] danack
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