Skip to content

Commit d586441

Browse files
committed
ReflectionClass::newInstanceWithoutConstructor() should be allowed to instantiate every class except those internal classes with a final __construct()
1 parent 43e956a commit d586441

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

ext/reflection/php_reflection.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4308,8 +4308,8 @@ ZEND_METHOD(reflection_class, newInstanceWithoutConstructor)
43084308
METHOD_NOTSTATIC(reflection_class_ptr);
43094309
GET_REFLECTION_OBJECT_PTR(ce);
43104310

4311-
if (ce->create_object != NULL) {
4312-
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Class %s is an internal class that cannot be instantiated without invoking its constructor", ce->name);
4311+
if (ce->create_object != NULL && ce->ce_flags & ZEND_ACC_FINAL_CLASS) {
4312+
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Class %s is an internal class with a final __construct thus cannot be instantiated without invoking its constructor", ce->name);
43134313
}
43144314

43154315
object_init_ex(return_value, ce);

ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ var_dump($class->newInstanceWithoutConstructor());
2020

2121
$class = new ReflectionClass('DateTime');
2222
var_dump($class->newInstanceWithoutConstructor());
23+
24+
$class = new ReflectionClass('Generator');
25+
var_dump($class->newInstanceWithoutConstructor());
2326
--EXPECTF--
2427
object(Foo)#%d (0) {
2528
}
2629
object(stdClass)#%d (0) {
2730
}
31+
object(DateTime)#%d (0) {
32+
}
2833

29-
Fatal error: Uncaught exception 'ReflectionException' with message 'Class DateTime is an internal class that cannot be instantiated without invoking its constructor' in %sReflectionClass_newInstanceWithoutConstructor.php:%d
34+
Fatal error: Uncaught exception 'ReflectionException' with message 'Class Generator is an internal class with a final __construct thus cannot be instantiated without invoking its constructor' in %sReflectionClass_newInstanceWithoutConstructor.php:%d
3035
Stack trace:
3136
#0 %sReflectionClass_newInstanceWithoutConstructor.php(%d): ReflectionClass->newInstanceWithoutConstructor()
3237
#1 {main}

ext/reflection/tests/bug64007.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ $generator = $reflection->newInstance();
1414
var_dump($generator);
1515
?>
1616
--EXPECTF--
17-
string(97) "Class Generator is an internal class that cannot be instantiated without invoking its constructor"
17+
string(%d) "Class Generator is an internal class with a final __construct thus cannot be instantiated without invoking its constructor"
1818

1919
Catchable fatal error: The "Generator" class is reserved for internal use and cannot be manually instantiated in %sbug64007.php on line %d

0 commit comments

Comments
 (0)