Re: [RFC] Callable constructors

From: Date: Fri, 26 Feb 2016 14:10:10 +0000
Subject: Re: [RFC] Callable constructors
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-91436@lists.php.net to get a copy of this message
On Fri, 2016-02-26 at 11:30 +0000, Andrea Faulds wrote: > This is true for a trivial implementation like the one above, but I > would point out that a more sophisticated userland implementation could > fix this by generating PHP code. You don't have to generate PHP code for this to work: $arr = is_string($func) ? explode("::", $func, 2) : $func; $ref = (sizeof($arr) === 2) ? new ReflectionMethod($arr[0], $arr[1]) : new ReflectionFunction($func); $args = identifyArgumentsAndGetCtorArgs($ref); if (sizeof($arr) === 2 && $arr[1] === '__construct') { $rc = new ReflectionClass($arr[0]); return $rc->newInstanceArgs($args); } else { return $ref->invokeArgs($args); } I haven't tested this and it might need some fine tuning, but isn't really complicated. (Of course you can generate PHP code for this trivially in order to avoid the small reflection overhead) > So it's still worth questioning whether this needs to be a built-in > function. I think it is quite obscure and a hardly used feature. Language features and syntax sugar things should be common things. johannes

Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc
« previous php.internals (#91436) next »