Skip to content

Commit 0789a35

Browse files
Define as Stringable::toString(): string
1 parent 01c6d2a commit 0789a35

18 files changed

+297
-29
lines changed

Zend/zend_exceptions.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,20 @@ ZEND_METHOD(exception, __toString)
754754
}
755755
/* }}} */
756756

757+
/* {{{ proto string Exception|Error::toString()
758+
Obtain the string representation of the Exception object */
759+
ZEND_METHOD(exception, toString)
760+
{
761+
zval rv;
762+
763+
ZEND_PARSE_PARAMETERS_NONE();
764+
765+
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), NULL, NULL, "__tostring", &rv);
766+
767+
ZVAL_COPY_VALUE(return_value, &rv);
768+
}
769+
/* }}} */
770+
757771
/** {{{ Throwable method definition */
758772
static const zend_function_entry zend_funcs_throwable[] = {
759773
ZEND_ABSTRACT_ME(throwable, getMessage, arginfo_class_Throwable_getMessage)
@@ -763,6 +777,7 @@ static const zend_function_entry zend_funcs_throwable[] = {
763777
ZEND_ABSTRACT_ME(throwable, getTrace, arginfo_class_Throwable_getTrace)
764778
ZEND_ABSTRACT_ME(throwable, getPrevious, arginfo_class_Throwable_getPrevious)
765779
ZEND_ABSTRACT_ME(throwable, getTraceAsString, arginfo_class_Throwable_getTraceAsString)
780+
ZEND_ABSTRACT_ME(throwable, __toString, arginfo_class_Throwable___toString)
766781
ZEND_FE_END
767782
};
768783
/* }}} */
@@ -789,6 +804,7 @@ static const zend_function_entry default_exception_functions[] = {
789804
ZEND_ME(exception, getPrevious, arginfo_class_Exception_getPrevious, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
790805
ZEND_ME(exception, getTraceAsString, arginfo_class_Exception_getTraceAsString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
791806
ZEND_ME(exception, __toString, arginfo_class_Exception___toString, 0)
807+
ZEND_ME(exception, toString, arginfo_class_Exception_toString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
792808
ZEND_FE_END
793809
};
794810

Zend/zend_exceptions.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function getPrevious();
2222

2323
/** @return string */
2424
function getTraceAsString();
25+
26+
/** @return string */
27+
function __toString();
2528
}
2629

2730
class Exception implements Throwable
@@ -53,6 +56,8 @@ final function getPrevious() {}
5356
/** @return string */
5457
final function getTraceAsString() {}
5558

59+
final function toString(): string {}
60+
5661
/** @return string */
5762
function __toString() {}
5863
}

Zend/zend_exceptions_arginfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ ZEND_END_ARG_INFO()
1515

1616
#define arginfo_class_Throwable_getTraceAsString arginfo_class_Throwable_getMessage
1717

18+
#define arginfo_class_Throwable___toString arginfo_class_Throwable_getMessage
19+
1820
#define arginfo_class_Exception___clone arginfo_class_Throwable_getMessage
1921

2022
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Exception___construct, 0, 0, 0)
@@ -41,6 +43,9 @@ ZEND_END_ARG_INFO()
4143

4244
#define arginfo_class_Exception___toString arginfo_class_Throwable_getMessage
4345

46+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Exception_toString, 0, 0, IS_STRING, 0)
47+
ZEND_END_ARG_INFO()
48+
4449
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ErrorException___construct, 0, 0, 0)
4550
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
4651
ZEND_ARG_TYPE_INFO(0, code, IS_LONG, 0)

Zend/zend_interfaces.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static const zend_function_entry zend_funcs_countable[] = {
577577
};
578578

579579
static const zend_function_entry zend_funcs_stringable[] = {
580-
ZEND_ABSTRACT_ME(Stringable, __toString, arginfo_class_Stringable___toString)
580+
ZEND_ABSTRACT_ME(Stringable, toString, arginfo_class_Stringable_toString)
581581
ZEND_FE_END
582582
};
583583
/* }}} */

Zend/zend_interfaces.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,5 @@ function count();
5252

5353
interface Stringable
5454
{
55-
/** @return string */
56-
function __toString();
55+
function toString(): string;
5756
}

Zend/zend_interfaces_arginfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ ZEND_END_ARG_INFO()
3434

3535
#define arginfo_class_Countable_count arginfo_class_IteratorAggregate_getIterator
3636

37-
#define arginfo_class_Stringable___toString arginfo_class_IteratorAggregate_getIterator
37+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Stringable_toString, 0, 0, IS_STRING, 0)
38+
ZEND_END_ARG_INFO()

Zend/zend_object_handlers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,11 @@ ZEND_API int zend_std_cast_object_tostring(zend_object *readobj, zval *writeobj,
17941794
zend_throw_error(NULL, "Method %s::__toString() must return a string value", ZSTR_VAL(ce->name));
17951795
}
17961796
}
1797+
if (zend_class_implements_interface(ce, zend_ce_stringable)) {
1798+
zend_call_method_with_0_params(readobj, NULL, NULL, "tostring", &retval);
1799+
ZVAL_COPY_VALUE(writeobj, &retval);
1800+
return SUCCESS;
1801+
}
17971802
return FAILURE;
17981803
case _IS_BOOL:
17991804
ZVAL_TRUE(writeobj);

ext/filter/filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static void php_zval_filter(zval *value, zend_long filter, zend_long flags, zval
345345
zend_class_entry *ce;
346346

347347
ce = Z_OBJCE_P(value);
348-
if (!ce->__tostring) {
348+
if (!ce->__tostring && !zend_class_implements_interface(ce, zend_ce_stringable)) {
349349
zval_ptr_dtor(value);
350350
/* #67167: doesn't return null on failure for objects */
351351
if (flags & FILTER_NULL_ON_FAILURE) {

ext/filter/php_filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ext/standard/php_string.h"
2727
#include "ext/standard/html.h"
2828
#include "php_variables.h"
29+
#include "zend_interfaces.h"
2930

3031
extern zend_module_entry filter_module_entry;
3132
#define phpext_filter_ptr &filter_module_entry

0 commit comments

Comments
 (0)