- Added ReflectionClass::isCloneable() [DOC]

This commit is contained in:
Felipe Pena 2010-06-02 15:29:42 +00:00
parent b03678348c
commit c98c39b1c1
4 changed files with 130 additions and 1 deletions

View File

@ -228,7 +228,8 @@ UPGRADE NOTES - PHP X.Y
h. New methods
-
- Reflection:
- ReflectionClass::isCloneable()
i. New class constants

View File

@ -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)

View File

@ -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

View File

@ -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)