Skip to content

Commit b04a052

Browse files
Close #55490.
1 parent d7d90c4 commit b04a052

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP NEWS
2323
- Improved NSAPI SAPI: (Uwe Schindler)
2424
. Don't set $_SERVER['HTTPS'] on unsecure connection (bug #55403).
2525

26+
- Improved Reflection extension:
27+
. Added ReflectionClass::newInstanceWithoutConstructor() to create a new
28+
instance of a class without invoking its constructor. FR #55490. (Sebastian)
29+
2630
04 Aug 2011, PHP 5.4.0 Alpha 3
2731
- Added features:
2832
. Short array syntax, see UPGRADING guide for full details (rsky0711 at gmail

ext/reflection/php_reflection.c

+23
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,25 @@ ZEND_METHOD(reflection_class, newInstance)
41294129
}
41304130
/* }}} */
41314131

4132+
/* {{{ proto public stdclass ReflectionClass::newInstanceWithoutConstructor()
4133+
Returns an instance of this class without invoking its constructor */
4134+
ZEND_METHOD(reflection_class, newInstanceWithoutConstructor)
4135+
{
4136+
zval *retval_ptr = NULL;
4137+
reflection_object *intern;
4138+
zend_class_entry *ce;
4139+
4140+
METHOD_NOTSTATIC(reflection_class_ptr);
4141+
GET_REFLECTION_OBJECT_PTR(ce);
4142+
4143+
if (ce->create_object != NULL) {
4144+
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);
4145+
}
4146+
4147+
object_init_ex(return_value, ce);
4148+
}
4149+
/* }}} */
4150+
41324151
/* {{{ proto public stdclass ReflectionClass::newInstanceArgs([array args])
41334152
Returns an instance of this class */
41344153
ZEND_METHOD(reflection_class, newInstanceArgs)
@@ -5694,6 +5713,9 @@ ZEND_BEGIN_ARG_INFO(arginfo_reflection_class_newInstance, 0)
56945713
ZEND_ARG_INFO(0, args)
56955714
ZEND_END_ARG_INFO()
56965715

5716+
ZEND_BEGIN_ARG_INFO(arginfo_reflection_class_newInstanceWithoutConstructor, 0)
5717+
ZEND_END_ARG_INFO()
5718+
56975719
ZEND_BEGIN_ARG_INFO_EX(arginfo_reflection_class_newInstanceArgs, 0, 0, 0)
56985720
ZEND_ARG_ARRAY_INFO(0, args, 0)
56995721
ZEND_END_ARG_INFO()
@@ -5742,6 +5764,7 @@ static const zend_function_entry reflection_class_functions[] = {
57425764
ZEND_ME(reflection_class, getModifiers, arginfo_reflection__void, 0)
57435765
ZEND_ME(reflection_class, isInstance, arginfo_reflection_class_isInstance, 0)
57445766
ZEND_ME(reflection_class, newInstance, arginfo_reflection_class_newInstance, 0)
5767+
ZEND_ME(reflection_class, newInstanceWithoutConstructor, arginfo_reflection_class_newInstanceWithoutConstructor, 0)
57455768
ZEND_ME(reflection_class, newInstanceArgs, arginfo_reflection_class_newInstanceArgs, 0)
57465769
ZEND_ME(reflection_class, getParentClass, arginfo_reflection__void, 0)
57475770
ZEND_ME(reflection_class, isSubclassOf, arginfo_reflection_class_isSubclassOf, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
ReflectionClass::newInstanceWithoutConstructor()
3+
--CREDITS--
4+
Sebastian Bergmann <sebastian@php.net>
5+
--FILE--
6+
<?php
7+
class Foo
8+
{
9+
public function __construct()
10+
{
11+
print __METHOD__;
12+
}
13+
}
14+
15+
$class = new ReflectionClass('Foo');
16+
var_dump($class->newInstanceWithoutConstructor());
17+
18+
$class = new ReflectionClass('StdClass');
19+
var_dump($class->newInstanceWithoutConstructor());
20+
21+
$class = new ReflectionClass('DateTime');
22+
var_dump($class->newInstanceWithoutConstructor());
23+
--EXPECTF--
24+
object(Foo)#%d (0) {
25+
}
26+
object(stdClass)#%d (0) {
27+
}
28+
29+
Fatal error: Uncaught exception 'ReflectionException' with message 'Class DateTime is an internal class that cannot be instantiated without invoking its constructor' in %s/tests/ReflectionClass_newInstanceWithoutConstructor.php:%d
30+
Stack trace:
31+
#0 %s/ReflectionClass_newInstanceWithoutConstructor.php(%d): ReflectionClass->newInstanceWithoutConstructor()
32+
#1 {main}
33+
thrown in %s/ReflectionClass_newInstanceWithoutConstructor.php on line %d

ext/reflection/tests/ReflectionClass_toString_001.phpt

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
3434
Property [ <default> public $name ]
3535
}
3636

37-
- Methods [48] {
37+
- Methods [49] {
3838
Method [ <internal:Reflection> final private method __clone ] {
3939

4040
- Parameters [0] {
@@ -250,6 +250,12 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
250250
}
251251
}
252252

253+
Method [ <internal:Reflection> public method newInstanceWithoutConstructor ] {
254+
255+
- Parameters [0] {
256+
}
257+
}
258+
253259
Method [ <internal:Reflection> public method newInstanceArgs ] {
254260

255261
- Parameters [1] {

0 commit comments

Comments
 (0)