Bug #66377 [Nab]: closure instantiation is not allowed when return string class name

From: Date: Wed, 01 Jan 2014 11:05:12 +0000
Subject: Bug #66377 [Nab]: closure instantiation is not allowed when return string class name
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183526@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66377&edit=1 ID: 66377 User updated by: dev at nicolab dot net Reported by: dev at nicolab dot net Summary: closure instantiation is not allowed when return string class name Status: Not a bug Type: Bug Package: Class/Object related Operating System: Debian 7 PHP Version: 5.4.23 Block user comment: N Private report: N New Comment: Thanks for your explanation. It seems logical to me that the $class variable is the instance of Closure and $class() is equivalent to the value returned (not the signature (Closure). As for the function parameter : $class = function(){ return '\StdClass'; }; $factory = function($class) { return new $class(); }; $factory($class()); // object(StdClass) Fortunately in this case $class parameter is the returned value of the Closure and not the signature (Closure). After I speak as a user of PHP, maybe in the PHP engine that concept ( new $class() ) does not make sense. In this case is not a bug but a feature request. happy New Year ! :) PS: sorry for my bad english Previous Comments: ------------------------------------------------------------------------ [2013-12-31 22:04:44] requinix@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php "new" only supports literal names, variable-variables (one level of indirection only), and object instances (as if you get_class()ed them). That means no closures, which are objects and thus why new tried to create an instance of Closure. Meanwhile the parentheses you're using are not to invoke the closure but to pass arguments to the class's constructor. $classname = $class(); new $classname(); ------------------------------------------------------------------------ [2013-12-31 21:28:39] dev at nicolab dot net Description: ------------ Hello, closure instantiation is not allowed when the closure return a string class name. My PHP package : $ php -v PHP 5.4.4-14+deb7u7 (cli) (built: Dec 12 2013 08:42:07) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans Test script: --------------- $class = function(){ return '\StdClass'; }; new $class(); Expected result: ---------------- (object) instance of \StdClass Actual result: -------------- Catchable fatal error: Instantiation of 'Closure' is not allowed in /var/www/file.php on line 30 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66377&edit=1

« previous php.bugs (#183526) next »