|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-12-31 21:28 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 21:00:01 2025 UTC |
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