Skip to content

Commit

Permalink
Add Stringable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 13, 2020
1 parent 8ee0494 commit d7a345c
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Zend/zend_exceptions.c
Expand Up @@ -812,7 +812,7 @@ void zend_register_default_exception(void) /* {{{ */
INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
zend_ce_exception->create_object = zend_default_exception_new;
zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
zend_class_implements(zend_ce_exception, 2, zend_ce_throwable, zend_ce_stringable);

zend_declare_property_string(zend_ce_exception, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
zend_declare_property_string(zend_ce_exception, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
Expand All @@ -830,7 +830,7 @@ void zend_register_default_exception(void) /* {{{ */
INIT_CLASS_ENTRY(ce, "Error", default_exception_functions);
zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
zend_ce_error->create_object = zend_default_exception_new;
zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
zend_class_implements(zend_ce_error, 2, zend_ce_throwable, zend_ce_stringable);

zend_declare_property_string(zend_ce_error, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
zend_declare_property_string(zend_ce_error, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
Expand Down
15 changes: 15 additions & 0 deletions Zend/zend_interfaces.c
Expand Up @@ -28,6 +28,7 @@ ZEND_API zend_class_entry *zend_ce_iterator;
ZEND_API zend_class_entry *zend_ce_arrayaccess;
ZEND_API zend_class_entry *zend_ce_serializable;
ZEND_API zend_class_entry *zend_ce_countable;
ZEND_API zend_class_entry *zend_ce_stringable;

/* {{{ zend_call_method
Only returns the returned zval if retval_ptr != NULL */
Expand Down Expand Up @@ -532,6 +533,13 @@ static int zend_implement_countable(zend_class_entry *interface, zend_class_entr
}
/* }}}*/

/* {{{ zend_implement_stringable */
static int zend_implement_stringable(zend_class_entry *interface, zend_class_entry *class_type)
{
return SUCCESS;
}
/* }}}*/

/* {{{ function tables */
static const zend_function_entry zend_funcs_aggregate[] = {
ZEND_ABSTRACT_ME(iterator, getIterator, arginfo_class_IteratorAggregate_getIterator)
Expand Down Expand Up @@ -567,6 +575,11 @@ static const zend_function_entry zend_funcs_countable[] = {
ZEND_ABSTRACT_ME(Countable, count, arginfo_class_Countable_count)
ZEND_FE_END
};

static const zend_function_entry zend_funcs_stringable[] = {
ZEND_ABSTRACT_ME(Stringable, __toString, arginfo_class_Stringable___toString)
ZEND_FE_END
};
/* }}} */

/* {{{ zend_register_interfaces */
Expand All @@ -585,5 +598,7 @@ ZEND_API void zend_register_interfaces(void)
REGISTER_MAGIC_INTERFACE(serializable, Serializable);

REGISTER_MAGIC_INTERFACE(countable, Countable);

REGISTER_MAGIC_INTERFACE(stringable, Stringable);
}
/* }}} */
1 change: 1 addition & 0 deletions Zend/zend_interfaces.h
Expand Up @@ -30,6 +30,7 @@ extern ZEND_API zend_class_entry *zend_ce_iterator;
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
extern ZEND_API zend_class_entry *zend_ce_serializable;
extern ZEND_API zend_class_entry *zend_ce_countable;
extern ZEND_API zend_class_entry *zend_ce_stringable;

typedef struct _zend_user_iterator {
zend_object_iterator it;
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_interfaces.stub.php
Expand Up @@ -49,3 +49,9 @@ interface Countable
/** @return int */
function count();
}

interface Stringable
{
/** @return string */
function __toString();
}
2 changes: 2 additions & 0 deletions Zend/zend_interfaces_arginfo.h
Expand Up @@ -33,3 +33,5 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Serializable_unserialize, 0, 0, 1)
ZEND_END_ARG_INFO()

#define arginfo_class_Countable_count arginfo_class_IteratorAggregate_getIterator

#define arginfo_class_Stringable___toString arginfo_class_IteratorAggregate_getIterator
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.c
Expand Up @@ -6643,6 +6643,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */

INIT_CLASS_ENTRY(_reflection_entry, "Reflector", reflector_functions);
reflector_ptr = zend_register_internal_interface(&_reflection_entry);
zend_class_implements(reflector_ptr, 1, zend_ce_stringable);

INIT_CLASS_ENTRY(_reflection_entry, "ReflectionFunctionAbstract", reflection_function_abstract_functions);
reflection_init_class_handlers(&_reflection_entry);
Expand Down Expand Up @@ -6671,6 +6672,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
reflection_init_class_handlers(&_reflection_entry);
reflection_type_ptr = zend_register_internal_class(&_reflection_entry);
reflection_type_ptr->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
zend_class_implements(reflection_type_ptr, 1, zend_ce_stringable);

INIT_CLASS_ENTRY(_reflection_entry, "ReflectionNamedType", reflection_named_type_functions);
reflection_init_class_handlers(&_reflection_entry);
Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/reflection.stub.php
Expand Up @@ -12,7 +12,7 @@ public static function getModifierNames(int $modifiers) {}
public static function export(Reflector $reflector, bool $return = false) {}
}

interface Reflector
interface Reflector extends Stringable
{
/** @return string */
public function __toString();
Expand Down Expand Up @@ -532,7 +532,7 @@ public function getDefaultValueConstantName() {}
public function isVariadic() {}
}

abstract class ReflectionType
abstract class ReflectionType implements Stringable
{
final private function __clone() {}

Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/tests/ReflectionClass_toString_001.phpt
Expand Up @@ -9,7 +9,7 @@ $rc = new ReflectionClass("ReflectionClass");
echo $rc;
?>
--EXPECT--
Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
Class [ <internal:Reflection> class ReflectionClass implements Reflector, Stringable ] {

- Constants [3] {
Constant [ public int IS_IMPLICIT_ABSTRACT ] { 16 }
Expand Down Expand Up @@ -48,7 +48,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
}
}

Method [ <internal:Reflection, prototype Reflector> public method __toString ] {
Method [ <internal:Reflection, prototype Stringable> public method __toString ] {

- Parameters [0] {
}
Expand Down
2 changes: 1 addition & 1 deletion ext/simplexml/simplexml.c
Expand Up @@ -2633,7 +2633,7 @@ PHP_MINIT_FUNCTION(simplexml)
sxe.create_object = sxe_object_new;
sxe_class_entry = zend_register_internal_class(&sxe);
sxe_class_entry->get_iterator = php_sxe_get_iterator;
zend_class_implements(sxe_class_entry, 2, zend_ce_traversable, zend_ce_countable);
zend_class_implements(sxe_class_entry, 3, zend_ce_traversable, zend_ce_countable, zend_ce_stringable);

memcpy(&sxe_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
sxe_object_handlers.offset = XtOffsetOf(php_sxe_object, zo);
Expand Down
1 change: 1 addition & 0 deletions ext/spl/spl_directory.c
Expand Up @@ -3104,6 +3104,7 @@ PHP_MINIT_FUNCTION(spl_directory)
spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;
REGISTER_SPL_IMPLEMENTS(SplFileInfo, Stringable);


REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, spl_filesystem_object_new, spl_DirectoryIterator_functions);
Expand Down
2 changes: 2 additions & 0 deletions ext/spl/spl_directory.h
Expand Up @@ -20,6 +20,8 @@
#include "php.h"
#include "php_spl.h"

#define spl_ce_Stringable zend_ce_stringable

extern PHPAPI zend_class_entry *spl_ce_SplFileInfo;
extern PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
extern PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
Expand Down
1 change: 1 addition & 0 deletions ext/spl/spl_iterators.c
Expand Up @@ -3669,6 +3669,7 @@ PHP_MINIT_FUNCTION(spl_iterators)
REGISTER_SPL_SUB_CLASS_EX(CachingIterator, IteratorIterator, spl_dual_it_new, spl_funcs_CachingIterator);
REGISTER_SPL_IMPLEMENTS(CachingIterator, ArrayAccess);
REGISTER_SPL_IMPLEMENTS(CachingIterator, Countable);
REGISTER_SPL_IMPLEMENTS(CachingIterator, Stringable);

REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CALL_TOSTRING", CIT_CALL_TOSTRING);
REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CATCH_GET_CHILD", CIT_CATCH_GET_CHILD);
Expand Down
1 change: 1 addition & 0 deletions ext/spl/spl_iterators.h
Expand Up @@ -27,6 +27,7 @@
#define spl_ce_ArrayAccess zend_ce_arrayaccess
#define spl_ce_Serializable zend_ce_serializable
#define spl_ce_Countable zend_ce_countable
#define spl_ce_Stringable zend_ce_stringable

extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
extern PHPAPI zend_class_entry *spl_ce_RecursiveIteratorIterator;
Expand Down
1 change: 0 additions & 1 deletion ext/zend_test/test.c
Expand Up @@ -237,7 +237,6 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_class_constant_long(zend_test_interface, ZEND_STRL("DUMMY"), 0);
INIT_CLASS_ENTRY(class_entry, "_ZendTestClass", zend_test_class_methods);
zend_test_class = zend_register_internal_class(&class_entry);
zend_class_implements(zend_test_class, 1, zend_test_interface);
zend_test_class->create_object = zend_test_class_new;
zend_test_class->get_static_method = zend_test_class_static_method_get;

Expand Down

0 comments on commit d7a345c

Please sign in to comment.