rfc:callableconstructors

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:callableconstructors [2016/02/25 13:01] – created danackrfc: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: 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 108: Line 108:
 </code> </code>
  
-If it was possible to use 'Foo::__construct' as a callable, that code would work. +If it was possible to use ''%%Foo::__construct%%'' as a callable, that code would work. 
  
 Currently because the constructor is not callable, extra code is needed. Currently because the constructor is not callable, extra code is needed.
Line 117: Line 117:
 $fooFactory = getenv('FooFactory'); $fooFactory = getenv('FooFactory');
  
-// $fooFactory is "Foo::_ _construct"; +// $fooFactory is "Foo::__construct"; 
-$constructorPosition = stripos($fooFactory, '::_ _construct');+$constructorPosition = stripos($fooFactory, '::__construct');
  
 if ($constructorPosition !== false) { if ($constructorPosition !== false) {
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.1456405270.txt.gz · Last modified: 2017/09/22 13:28 (external edit)