php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66377 closure instantiation is not allowed when return string class name
Submitted: 2013-12-31 21:28 UTC Modified: 2014-01-01 11:12 UTC
From: dev at nicolab dot net Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.23 OS: Debian 7
Private report: No CVE-ID: None
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-31 22:04 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2013-12-31 22:04 UTC] 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();
 [2014-01-01 11:05 UTC] dev at nicolab dot net
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
 [2014-01-01 11:12 UTC] dev at nicolab dot net
-Type: Bug +Type: Feature/Change Request
 [2014-01-01 11:12 UTC] dev at nicolab dot net
I change the bug report to feature request.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC