sebastian Fri, 26 Aug 2011 07:40:31 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=315538
Log: Close #55490. Bug: https://bugs.php.net/55490 (Open) Allow instantiating objects without invoking the constructor Changed paths: U php/php-src/branches/PHP_5_4/NEWS U php/php-src/branches/PHP_5_4/ext/reflection/php_reflection.c A php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt U php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_toString_001.phpt U php/php-src/trunk/ext/reflection/php_reflection.c A php/php-src/trunk/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt U php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt
Modified: php/php-src/branches/PHP_5_4/NEWS =================================================================== --- php/php-src/branches/PHP_5_4/NEWS 2011-08-26 07:34:58 UTC (rev 315537) +++ php/php-src/branches/PHP_5_4/NEWS 2011-08-26 07:40:31 UTC (rev 315538) @@ -23,6 +23,10 @@ - Improved NSAPI SAPI: (Uwe Schindler) . Don't set $_SERVER['HTTPS'] on unsecure connection (bug #55403). +- Improved Reflection extension: + . Added ReflectionClass::newInstanceWithoutConstructor() to create a new + instance of a class without invoking its constructor. FR #55490. (Sebastian) + 04 Aug 2011, PHP 5.4.0 Alpha 3 - Added features: . Short array syntax, see UPGRADING guide for full details (rsky0711 at gmail Modified: php/php-src/branches/PHP_5_4/ext/reflection/php_reflection.c =================================================================== --- php/php-src/branches/PHP_5_4/ext/reflection/php_reflection.c 2011-08-26 07:34:58 UTC (rev 315537) +++ php/php-src/branches/PHP_5_4/ext/reflection/php_reflection.c 2011-08-26 07:40:31 UTC (rev 315538) @@ -4129,6 +4129,25 @@ } /* }}} */ +/* {{{ proto public stdclass ReflectionClass::newInstanceWithoutConstructor() + Returns an instance of this class without invoking its constructor */ +ZEND_METHOD(reflection_class, newInstanceWithoutConstructor) +{ + zval *retval_ptr = NULL; + reflection_object *intern; + zend_class_entry *ce; + + METHOD_NOTSTATIC(reflection_class_ptr); + GET_REFLECTION_OBJECT_PTR(ce); + + if (ce->create_object != NULL) { + 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); + } + + object_init_ex(return_value, ce); +} +/* }}} */ + /* {{{ proto public stdclass ReflectionClass::newInstanceArgs([array args]) Returns an instance of this class */ ZEND_METHOD(reflection_class, newInstanceArgs) @@ -5694,6 +5713,9 @@ ZEND_ARG_INFO(0, args) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_reflection_class_newInstanceWithoutConstructor, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_reflection_class_newInstanceArgs, 0, 0, 0) ZEND_ARG_ARRAY_INFO(0, args, 0) ZEND_END_ARG_INFO() @@ -5742,6 +5764,7 @@ ZEND_ME(reflection_class, getModifiers, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isInstance, arginfo_reflection_class_isInstance, 0) ZEND_ME(reflection_class, newInstance, arginfo_reflection_class_newInstance, 0) + ZEND_ME(reflection_class, newInstanceWithoutConstructor, arginfo_reflection_class_newInstanceWithoutConstructor, 0) ZEND_ME(reflection_class, newInstanceArgs, arginfo_reflection_class_newInstanceArgs, 0) ZEND_ME(reflection_class, getParentClass, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isSubclassOf, arginfo_reflection_class_isSubclassOf, 0) Added: php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt =================================================================== --- php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt (rev 0) +++ php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt 2011-08-26 07:40:31 UTC (rev 315538) @@ -0,0 +1,33 @@ +--TEST-- +ReflectionClass::newInstanceWithoutConstructor() +--CREDITS-- +Sebastian Bergmann <sebast...@php.net> +--FILE-- +<?php +class Foo +{ + public function __construct() + { + print __METHOD__; + } +} + +$class = new ReflectionClass('Foo'); +var_dump($class->newInstanceWithoutConstructor()); + +$class = new ReflectionClass('StdClass'); +var_dump($class->newInstanceWithoutConstructor()); + +$class = new ReflectionClass('DateTime'); +var_dump($class->newInstanceWithoutConstructor()); +--EXPECTF-- +object(Foo)#%d (0) { +} +object(stdClass)#%d (0) { +} + +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 +Stack trace: +#0 %s/ReflectionClass_newInstanceWithoutConstructor.php(%d): ReflectionClass->newInstanceWithoutConstructor() +#1 {main} + thrown in %s/ReflectionClass_newInstanceWithoutConstructor.php on line %d Property changes on: php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt ___________________________________________________________________ Added: svn:keywords + Id Rev Revision Added: svn:eol-style + native Modified: php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_toString_001.phpt =================================================================== --- php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_toString_001.phpt 2011-08-26 07:34:58 UTC (rev 315537) +++ php/php-src/branches/PHP_5_4/ext/reflection/tests/ReflectionClass_toString_001.phpt 2011-08-26 07:40:31 UTC (rev 315538) @@ -34,7 +34,7 @@ Property [ <default> public $name ] } - - Methods [48] { + - Methods [49] { Method [ <internal:Reflection> final private method __clone ] { - Parameters [0] { @@ -250,6 +250,12 @@ } } + Method [ <internal:Reflection> public method newInstanceWithoutConstructor ] { + + - Parameters [0] { + } + } + Method [ <internal:Reflection> public method newInstanceArgs ] { - Parameters [1] { Modified: php/php-src/trunk/ext/reflection/php_reflection.c =================================================================== --- php/php-src/trunk/ext/reflection/php_reflection.c 2011-08-26 07:34:58 UTC (rev 315537) +++ php/php-src/trunk/ext/reflection/php_reflection.c 2011-08-26 07:40:31 UTC (rev 315538) @@ -4129,6 +4129,25 @@ } /* }}} */ +/* {{{ proto public stdclass ReflectionClass::newInstanceWithoutConstructor() + Returns an instance of this class without invoking its constructor */ +ZEND_METHOD(reflection_class, newInstanceWithoutConstructor) +{ + zval *retval_ptr = NULL; + reflection_object *intern; + zend_class_entry *ce; + + METHOD_NOTSTATIC(reflection_class_ptr); + GET_REFLECTION_OBJECT_PTR(ce); + + if (ce->create_object != NULL) { + 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); + } + + object_init_ex(return_value, ce); +} +/* }}} */ + /* {{{ proto public stdclass ReflectionClass::newInstanceArgs([array args]) Returns an instance of this class */ ZEND_METHOD(reflection_class, newInstanceArgs) @@ -5694,6 +5713,9 @@ ZEND_ARG_INFO(0, args) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_reflection_class_newInstanceWithoutConstructor, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_reflection_class_newInstanceArgs, 0, 0, 0) ZEND_ARG_ARRAY_INFO(0, args, 0) ZEND_END_ARG_INFO() @@ -5742,6 +5764,7 @@ ZEND_ME(reflection_class, getModifiers, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isInstance, arginfo_reflection_class_isInstance, 0) ZEND_ME(reflection_class, newInstance, arginfo_reflection_class_newInstance, 0) + ZEND_ME(reflection_class, newInstanceWithoutConstructor, arginfo_reflection_class_newInstanceWithoutConstructor, 0) ZEND_ME(reflection_class, newInstanceArgs, arginfo_reflection_class_newInstanceArgs, 0) ZEND_ME(reflection_class, getParentClass, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isSubclassOf, arginfo_reflection_class_isSubclassOf, 0) Added: php/php-src/trunk/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt =================================================================== --- php/php-src/trunk/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt (rev 0) +++ php/php-src/trunk/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt 2011-08-26 07:40:31 UTC (rev 315538) @@ -0,0 +1,33 @@ +--TEST-- +ReflectionClass::newInstanceWithoutConstructor() +--CREDITS-- +Sebastian Bergmann <sebast...@php.net> +--FILE-- +<?php +class Foo +{ + public function __construct() + { + print __METHOD__; + } +} + +$class = new ReflectionClass('Foo'); +var_dump($class->newInstanceWithoutConstructor()); + +$class = new ReflectionClass('StdClass'); +var_dump($class->newInstanceWithoutConstructor()); + +$class = new ReflectionClass('DateTime'); +var_dump($class->newInstanceWithoutConstructor()); +--EXPECTF-- +object(Foo)#%d (0) { +} +object(stdClass)#%d (0) { +} + +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 +Stack trace: +#0 %s/ReflectionClass_newInstanceWithoutConstructor.php(%d): ReflectionClass->newInstanceWithoutConstructor() +#1 {main} + thrown in %s/ReflectionClass_newInstanceWithoutConstructor.php on line %d Property changes on: php/php-src/trunk/ext/reflection/tests/ReflectionClass_newInstanceWithoutConstructor.phpt ___________________________________________________________________ Added: svn:keywords + Id Rev Revision Added: svn:eol-style + native Modified: php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt =================================================================== --- php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt 2011-08-26 07:34:58 UTC (rev 315537) +++ php/php-src/trunk/ext/reflection/tests/ReflectionClass_toString_001.phpt 2011-08-26 07:40:31 UTC (rev 315538) @@ -34,7 +34,7 @@ Property [ <default> public $name ] } - - Methods [48] { + - Methods [49] { Method [ <internal:Reflection> final private method __clone ] { - Parameters [0] { @@ -250,6 +250,12 @@ } } + Method [ <internal:Reflection> public method newInstanceWithoutConstructor ] { + + - Parameters [0] { + } + } + Method [ <internal:Reflection> public method newInstanceArgs ] { - Parameters [1] {
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php