diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index 1e344cde436ff..f074389c6f38e 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -677,6 +677,11 @@ zend_result php_json_encode_zval(smart_str *buf, zval *val, int options, php_jso if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) { return php_json_encode_serializable_enum(buf, val, options, encoder); } + if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) { + if (!(Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ANON_CLASS) || (Z_OBJCE_P(val)->parent && Z_OBJCE_P(val)->parent->ce_flags & ZEND_ACC_NOT_SERIALIZABLE)) { + zend_error(E_DEPRECATED, "json_encode() of non-serializable class %s is deprecated", ZSTR_VAL(Z_OBJCE_P(val)->name)); + } + } /* fallthrough -- Non-serializable object */ ZEND_FALLTHROUGH; case IS_ARRAY: { diff --git a/tests/classes/json_encode_non_serializable_deprecated.phpt b/tests/classes/json_encode_non_serializable_deprecated.phpt new file mode 100644 index 0000000000000..a1673373ef5fd --- /dev/null +++ b/tests/classes/json_encode_non_serializable_deprecated.phpt @@ -0,0 +1,24 @@ +--TEST-- +json_encode() on instances of classes market with ZEND_ACC_NOT_SERIALIZABLE +--FILE-- + +--EXPECTF-- +Deprecated: json_encode() of non-serializable class Generator is deprecated in %s on line %d +{} +Deprecated: json_encode() of non-serializable class Closure is deprecated in %s on line %d +{} +Deprecated: json_encode() of non-serializable class PDOStatement@anonymous is deprecated in %s on line %d +{}{}{}