Skip to content

Add method to ReflectionClass to instantiate with the same semantics as PDOStatement::fetchObject() #19401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -5127,6 +5127,67 @@ ZEND_METHOD(ReflectionClass, newInstanceArgs)
}
/* }}} */

/* {{{ Returns an instance of this class whose properties are filled with the given data before the constructor is called */
ZEND_METHOD(ReflectionClass, newInstanceFromData)
{
reflection_object *intern;
zend_class_entry *ce;
int argc = 0;
HashTable *data, *args = NULL;
zend_function *constructor;
zend_string *key;
zval *val;

GET_REFLECTION_OBJECT_PTR(ce);

if (ce->type == ZEND_INTERNAL_CLASS) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s is an internal class that cannot be instantiated from data", ZSTR_VAL(ce->name));
RETURN_THROWS();
}

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_HT(data)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(args)
ZEND_PARSE_PARAMETERS_END();

if (args) {
argc = zend_hash_num_elements(args);
}

if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
return;
}

ZEND_HASH_FOREACH_STR_KEY_VAL(data, key, val) {
zend_update_property_ex(ce, Z_OBJ_P(return_value), key, val);
} ZEND_HASH_FOREACH_END();

const zend_class_entry *old_scope = EG(fake_scope);
EG(fake_scope) = ce;
constructor = Z_OBJ_HT_P(return_value)->get_constructor(Z_OBJ_P(return_value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may throw for internal classes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same code used by newInstance and newInstanceArgs which I'm thinking of refactoring into a shared function to cut the duplication, so would make sense to handle instantiating internal classes there but could be considered a breaking change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether that's really a BC break? It would be throwing an exception either way?

We had a discussion a while ago with @DanielEScherzer and I think @nielsdos that it would probably be better to do something to fix the semantics regarding failing get_constructor, as various other places assume that you can even just check the constructor pointer on the CE.

Probably the best solution is that if the constructor field on the CE is NULL it means that it is not instantiable, and to set that field for classes that do not have a constructor to the zend_pass_function (which is what get's "called" anyway in those cases) but the downside of this approach is that you stop getting useful errors telling you how to get such an object (e.g. curl_init() for CurlHandle objects)

Copy link
Author

@mattdinthehouse mattdinthehouse Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean - because this method sets properties before calling constructors that can have weird (and potentially vulnerable) behaviour.

I've added a check for ce->type == ZEND_INTERNAL_CLASS similar to newInstanceWithoutConstructor() to prevent using newInstanceFromData() for internal classes entirely.

EG(fake_scope) = old_scope;

/* Run the constructor if there is one */
if (constructor) {
if (!(constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Access to non-public constructor of class %s", ZSTR_VAL(ce->name));
zval_ptr_dtor(return_value);
RETURN_NULL();
}

zend_call_known_function(
constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value), NULL, 0, NULL, args);

if (EG(exception)) {
zend_object_store_ctor_failed(Z_OBJ_P(return_value));
}
} else if (argc) {
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s does not have a constructor, so you cannot pass any constructor arguments", ZSTR_VAL(ce->name));
}
}
/* }}} */

void reflection_class_new_lazy(INTERNAL_FUNCTION_PARAMETERS,
int strategy, bool is_reset)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ public function newInstanceWithoutConstructor(): object {}
/** @tentative-return-type */
public function newInstanceArgs(array $args = []): ?object {}

public function newInstanceFromData(array $data, array $args = []): ?object {}

public function newLazyGhost(callable $initializer, int $options = 0): object {}

public function newLazyProxy(callable $factory, int $options = 0): object {}
Expand Down
9 changes: 8 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
--TEST--
ReflectionClass::newInstanceFromData
--FILE--
<?php

class A
{
public int $a;
public string $b;

public function __construct($c, $d)
{
echo "In constructor of class A\n";
}
}

class B
{
public int $a;
public string $b;
}

class C
{
}

#[\AllowDynamicProperties]
class D
{
}


$rcA = new ReflectionClass('A');
$rcB = new ReflectionClass('B');
$rcC = new ReflectionClass('C');
$rcD = new ReflectionClass('D');

try
{
$rcA->newInstanceFromData(['a' => 'bad', 'b' => 123], ['foo', 1337]);
}
catch(Throwable $e)
{
echo "Exception: " . $e->getMessage() . "\n";
}

var_dump($rcA->newInstanceFromData(['a' => 123, 'b' => 'good'], ['foo', 1337]));

var_dump($rcB->newInstanceFromData(['a' => 123, 'b' => 'good']));

var_dump($rcC->newInstanceFromData(['a' => 123, 'b' => 'good']));

var_dump($rcC->newInstanceFromData([]));

var_dump($rcD->newInstanceFromData(['a' => 123, 'b' => 'good']));

?>
--EXPECTF--
Exception: Cannot assign string to property A::$a of type int
In constructor of class A
object(A)#5 (2) {
["a"]=>
int(123)
["b"]=>
string(4) "good"
}
object(B)#5 (2) {
["a"]=>
int(123)
["b"]=>
string(4) "good"
}

Deprecated: Creation of dynamic property C::$a is deprecated in %s on line %d

Deprecated: Creation of dynamic property C::$b is deprecated in %s on line %d
object(C)#5 (2) {
["a"]=>
int(123)
["b"]=>
string(4) "good"
}
object(C)#5 (0) {
}
object(D)#5 (2) {
["a"]=>
int(123)
["b"]=>
string(4) "good"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
ReflectionClass::newInstanceFromData - internal class
--FILE--
<?php

$rcDateTime = new ReflectionClass('DateTime');
$rcPDOStatement = new ReflectionClass('PDOStatement');

try
{
$rcDateTime->newInstanceFromData([], ['now', new DateTimeZone('UTC')]);
}
catch(Throwable $e)
{
echo "Exception: " . $e->getMessage() . "\n";
}

try
{
$rcPDOStatement->newInstanceFromData(['a' => 123]);
}
catch(Throwable $e)
{
echo "Exception: " . $e->getMessage() . "\n";
}

?>
--EXPECTF--
Exception: Class DateTime is an internal class that cannot be instantiated from data
Exception: Class PDOStatement is an internal class that cannot be instantiated from data
11 changes: 10 additions & 1 deletion ext/reflection/tests/ReflectionClass_toString_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Stringable, Refle
Property [ public string $name ]
}

- Methods [64] {
- Methods [65] {
Method [ <internal:Reflection> private method __clone ] {

- Parameters [0] {
Expand Down Expand Up @@ -332,6 +332,15 @@ Class [ <internal:Reflection> class ReflectionClass implements Stringable, Refle
- Tentative return [ ?object ]
}

Method [ <internal:Reflection> public method newInstanceFromData ] {

- Parameters [2] {
Parameter #0 [ <required> array $data ]
Parameter #1 [ <optional> array $args = [] ]
}
- Return [ ?object ]
}

Method [ <internal:Reflection> public method newLazyGhost ] {

- Parameters [2] {
Expand Down