Skip to content

Commit d7a345c

Browse files
Add Stringable interface
1 parent 8ee0494 commit d7a345c

14 files changed

+38
-8
lines changed

Zend/zend_exceptions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ void zend_register_default_exception(void) /* {{{ */
812812
INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
813813
zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
814814
zend_ce_exception->create_object = zend_default_exception_new;
815-
zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
815+
zend_class_implements(zend_ce_exception, 2, zend_ce_throwable, zend_ce_stringable);
816816

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

835835
zend_declare_property_string(zend_ce_error, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
836836
zend_declare_property_string(zend_ce_error, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);

Zend/zend_interfaces.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ZEND_API zend_class_entry *zend_ce_iterator;
2828
ZEND_API zend_class_entry *zend_ce_arrayaccess;
2929
ZEND_API zend_class_entry *zend_ce_serializable;
3030
ZEND_API zend_class_entry *zend_ce_countable;
31+
ZEND_API zend_class_entry *zend_ce_stringable;
3132

3233
/* {{{ zend_call_method
3334
Only returns the returned zval if retval_ptr != NULL */
@@ -532,6 +533,13 @@ static int zend_implement_countable(zend_class_entry *interface, zend_class_entr
532533
}
533534
/* }}}*/
534535

536+
/* {{{ zend_implement_stringable */
537+
static int zend_implement_stringable(zend_class_entry *interface, zend_class_entry *class_type)
538+
{
539+
return SUCCESS;
540+
}
541+
/* }}}*/
542+
535543
/* {{{ function tables */
536544
static const zend_function_entry zend_funcs_aggregate[] = {
537545
ZEND_ABSTRACT_ME(iterator, getIterator, arginfo_class_IteratorAggregate_getIterator)
@@ -567,6 +575,11 @@ static const zend_function_entry zend_funcs_countable[] = {
567575
ZEND_ABSTRACT_ME(Countable, count, arginfo_class_Countable_count)
568576
ZEND_FE_END
569577
};
578+
579+
static const zend_function_entry zend_funcs_stringable[] = {
580+
ZEND_ABSTRACT_ME(Stringable, __toString, arginfo_class_Stringable___toString)
581+
ZEND_FE_END
582+
};
570583
/* }}} */
571584

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

587600
REGISTER_MAGIC_INTERFACE(countable, Countable);
601+
602+
REGISTER_MAGIC_INTERFACE(stringable, Stringable);
588603
}
589604
/* }}} */

Zend/zend_interfaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern ZEND_API zend_class_entry *zend_ce_iterator;
3030
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
3131
extern ZEND_API zend_class_entry *zend_ce_serializable;
3232
extern ZEND_API zend_class_entry *zend_ce_countable;
33+
extern ZEND_API zend_class_entry *zend_ce_stringable;
3334

3435
typedef struct _zend_user_iterator {
3536
zend_object_iterator it;

Zend/zend_interfaces.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ interface Countable
4949
/** @return int */
5050
function count();
5151
}
52+
53+
interface Stringable
54+
{
55+
/** @return string */
56+
function __toString();
57+
}

Zend/zend_interfaces_arginfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Serializable_unserialize, 0, 0, 1)
3333
ZEND_END_ARG_INFO()
3434

3535
#define arginfo_class_Countable_count arginfo_class_IteratorAggregate_getIterator
36+
37+
#define arginfo_class_Stringable___toString arginfo_class_IteratorAggregate_getIterator

ext/reflection/php_reflection.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6643,6 +6643,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
66436643

66446644
INIT_CLASS_ENTRY(_reflection_entry, "Reflector", reflector_functions);
66456645
reflector_ptr = zend_register_internal_interface(&_reflection_entry);
6646+
zend_class_implements(reflector_ptr, 1, zend_ce_stringable);
66466647

66476648
INIT_CLASS_ENTRY(_reflection_entry, "ReflectionFunctionAbstract", reflection_function_abstract_functions);
66486649
reflection_init_class_handlers(&_reflection_entry);
@@ -6671,6 +6672,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
66716672
reflection_init_class_handlers(&_reflection_entry);
66726673
reflection_type_ptr = zend_register_internal_class(&_reflection_entry);
66736674
reflection_type_ptr->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
6675+
zend_class_implements(reflection_type_ptr, 1, zend_ce_stringable);
66746676

66756677
INIT_CLASS_ENTRY(_reflection_entry, "ReflectionNamedType", reflection_named_type_functions);
66766678
reflection_init_class_handlers(&_reflection_entry);

ext/reflection/reflection.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static function getModifierNames(int $modifiers) {}
1212
public static function export(Reflector $reflector, bool $return = false) {}
1313
}
1414

15-
interface Reflector
15+
interface Reflector extends Stringable
1616
{
1717
/** @return string */
1818
public function __toString();
@@ -532,7 +532,7 @@ public function getDefaultValueConstantName() {}
532532
public function isVariadic() {}
533533
}
534534

535-
abstract class ReflectionType
535+
abstract class ReflectionType implements Stringable
536536
{
537537
final private function __clone() {}
538538

ext/reflection/tests/ReflectionClass_toString_001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $rc = new ReflectionClass("ReflectionClass");
99
echo $rc;
1010
?>
1111
--EXPECT--
12-
Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
12+
Class [ <internal:Reflection> class ReflectionClass implements Reflector, Stringable ] {
1313

1414
- Constants [3] {
1515
Constant [ public int IS_IMPLICIT_ABSTRACT ] { 16 }
@@ -48,7 +48,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
4848
}
4949
}
5050

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

5353
- Parameters [0] {
5454
}

ext/simplexml/simplexml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ PHP_MINIT_FUNCTION(simplexml)
26332633
sxe.create_object = sxe_object_new;
26342634
sxe_class_entry = zend_register_internal_class(&sxe);
26352635
sxe_class_entry->get_iterator = php_sxe_get_iterator;
2636-
zend_class_implements(sxe_class_entry, 2, zend_ce_traversable, zend_ce_countable);
2636+
zend_class_implements(sxe_class_entry, 3, zend_ce_traversable, zend_ce_countable, zend_ce_stringable);
26372637

26382638
memcpy(&sxe_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
26392639
sxe_object_handlers.offset = XtOffsetOf(php_sxe_object, zo);

ext/spl/spl_directory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,7 @@ PHP_MINIT_FUNCTION(spl_directory)
31043104
spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
31053105
spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
31063106
spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;
3107+
REGISTER_SPL_IMPLEMENTS(SplFileInfo, Stringable);
31073108

31083109

31093110
REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, spl_filesystem_object_new, spl_DirectoryIterator_functions);

ext/spl/spl_directory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "php.h"
2121
#include "php_spl.h"
2222

23+
#define spl_ce_Stringable zend_ce_stringable
24+
2325
extern PHPAPI zend_class_entry *spl_ce_SplFileInfo;
2426
extern PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
2527
extern PHPAPI zend_class_entry *spl_ce_FilesystemIterator;

ext/spl/spl_iterators.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3669,6 +3669,7 @@ PHP_MINIT_FUNCTION(spl_iterators)
36693669
REGISTER_SPL_SUB_CLASS_EX(CachingIterator, IteratorIterator, spl_dual_it_new, spl_funcs_CachingIterator);
36703670
REGISTER_SPL_IMPLEMENTS(CachingIterator, ArrayAccess);
36713671
REGISTER_SPL_IMPLEMENTS(CachingIterator, Countable);
3672+
REGISTER_SPL_IMPLEMENTS(CachingIterator, Stringable);
36723673

36733674
REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CALL_TOSTRING", CIT_CALL_TOSTRING);
36743675
REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CATCH_GET_CHILD", CIT_CATCH_GET_CHILD);

ext/spl/spl_iterators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define spl_ce_ArrayAccess zend_ce_arrayaccess
2828
#define spl_ce_Serializable zend_ce_serializable
2929
#define spl_ce_Countable zend_ce_countable
30+
#define spl_ce_Stringable zend_ce_stringable
3031

3132
extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
3233
extern PHPAPI zend_class_entry *spl_ce_RecursiveIteratorIterator;

ext/zend_test/test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ PHP_MINIT_FUNCTION(zend_test)
237237
zend_declare_class_constant_long(zend_test_interface, ZEND_STRL("DUMMY"), 0);
238238
INIT_CLASS_ENTRY(class_entry, "_ZendTestClass", zend_test_class_methods);
239239
zend_test_class = zend_register_internal_class(&class_entry);
240-
zend_class_implements(zend_test_class, 1, zend_test_interface);
241240
zend_test_class->create_object = zend_test_class_new;
242241
zend_test_class->get_static_method = zend_test_class_static_method_get;
243242

0 commit comments

Comments
 (0)