diff options
author | Felipe Pena <felipe@php.net> | 2010-06-02 15:29:42 +0000 |
---|---|---|
committer | Felipe Pena <felipe@php.net> | 2010-06-02 15:29:42 +0000 |
commit | c98c39b1c11221ae8de384942b75a4106d0c7f37 (patch) | |
tree | fcecc984b7f25d6f2c28a2d2167715541150194d /ext | |
parent | b03678348c43797f74a8097933a958cc300609ae (diff) | |
download | php-git-c98c39b1c11221ae8de384942b75a4106d0c7f37.tar.gz |
- Added ReflectionClass::isCloneable() [DOC]
Diffstat (limited to 'ext')
-rw-r--r-- | ext/reflection/php_reflection.c | 34 | ||||
-rw-r--r-- | ext/reflection/tests/ReflectionClass_isCloneable_001.phpt | 69 | ||||
-rw-r--r-- | ext/reflection/tests/ReflectionClass_isCloneable_002.phpt | 25 |
3 files changed, 128 insertions, 0 deletions
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a3aaf3eea1..6bf04df414 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3987,6 +3987,39 @@ ZEND_METHOD(reflection_class, isInstantiable) } /* }}} */ +/* {{{ proto public bool ReflectionClass::isCloneable() + Returns whether this class is cloneable */ +ZEND_METHOD(reflection_class, isCloneable) +{ + reflection_object *intern; + zend_class_entry *ce; + zval obj; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + GET_REFLECTION_OBJECT_PTR(ce); + if (ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS)) { + RETURN_FALSE; + } + if (intern->obj) { + if (ce->clone) { + RETURN_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC); + } else { + RETURN_BOOL(Z_OBJ_HANDLER_P(intern->obj, clone_obj) != NULL); + } + } else { + object_init_ex(&obj, ce); + if (ce->clone) { + RETVAL_BOOL(ce->clone->common.fn_flags & ZEND_ACC_PUBLIC); + } else { + RETVAL_BOOL(Z_OBJ_HANDLER(obj, clone_obj) != NULL); + } + zval_dtor(&obj); + } +} +/* }}} */ + /* {{{ proto public bool ReflectionClass::isInterface() Returns whether this is an interface or a class */ ZEND_METHOD(reflection_class, isInterface) @@ -5693,6 +5726,7 @@ static const zend_function_entry reflection_class_functions[] = { ZEND_ME(reflection_class, isInternal, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isUserDefined, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isInstantiable, arginfo_reflection__void, 0) + ZEND_ME(reflection_class, isCloneable, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getFileName, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getStartLine, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getEndLine, arginfo_reflection__void, 0) diff --git a/ext/reflection/tests/ReflectionClass_isCloneable_001.phpt b/ext/reflection/tests/ReflectionClass_isCloneable_001.phpt new file mode 100644 index 0000000000..0f5278c9b0 --- /dev/null +++ b/ext/reflection/tests/ReflectionClass_isCloneable_001.phpt @@ -0,0 +1,69 @@ +--TEST-- +Testing ReflectionClass::isCloneable() +--FILE-- +<?php + +class foo { +} +$foo = new foo; + +print "User class\n"; +$obj = new ReflectionClass($foo); +var_dump($obj->isCloneable()); +$obj = new ReflectionObject($foo); +var_dump($obj->isCloneable()); +$h = clone $foo; + +class bar { + private function __clone() { + } +} +$bar = new bar; +print "User class - private __clone\n"; +$obj = new ReflectionClass($bar); +var_dump($obj->isCloneable()); +$obj = new ReflectionObject($bar); +var_dump($obj->isCloneable()); +$h = clone $foo; + +print "Closure\n"; +$closure = function () { }; +$obj = new ReflectionClass($closure); +var_dump($obj->isCloneable()); +$obj = new ReflectionObject($closure); +var_dump($obj->isCloneable()); +$h = clone $closure; + +print "Internal class - SimpleXMLElement\n"; +$obj = new ReflectionClass('simplexmlelement'); +var_dump($obj->isCloneable()); +$obj = new ReflectionObject(new simplexmlelement('<test></test>')); +var_dump($obj->isCloneable()); +$h = clone new simplexmlelement('<test></test>'); + +print "Internal class - XMLWriter\n"; +$obj = new ReflectionClass('xmlwriter'); +var_dump($obj->isCloneable()); +$obj = new ReflectionObject(new XMLWriter); +var_dump($obj->isCloneable()); +$h = clone new xmlwriter; + +?> +--EXPECTF-- +User class +bool(true) +bool(true) +User class - private __clone +bool(false) +bool(false) +Closure +bool(true) +bool(true) +Internal class - SimpleXMLElement +bool(true) +bool(true) +Internal class - XMLWriter +bool(false) +bool(false) + +Fatal error: Trying to clone an uncloneable object of class XMLWriter in %s on line %d diff --git a/ext/reflection/tests/ReflectionClass_isCloneable_002.phpt b/ext/reflection/tests/ReflectionClass_isCloneable_002.phpt new file mode 100644 index 0000000000..7cde5768cc --- /dev/null +++ b/ext/reflection/tests/ReflectionClass_isCloneable_002.phpt @@ -0,0 +1,25 @@ +--TEST-- +Testing ReflectionClass::isCloneable() with non instantiable objects +--FILE-- +<?php + +trait foo { +} +$obj = new ReflectionClass('foo'); +var_dump($obj->isCloneable()); + +abstract class bar { +} +$obj = new ReflectionClass('bar'); +var_dump($obj->isCloneable()); + +interface baz { +} +$obj = new ReflectionClass('baz'); +var_dump($obj->isCloneable()); + +?> +--EXPECT-- +bool(false) +bool(false) +bool(false) |