Req #79972 [Nab]: Implement JsonSerializable interface in stdClass

From: Date: Thu, 13 Aug 2020 13:31:22 +0000
Subject: Req #79972 [Nab]: Implement JsonSerializable interface in stdClass
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228576@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79972&edit=1

 ID:                 79972
 Updated by:         nikic@php.net
 Reported by:        fodor dot laszlo at gmail dot com
 Summary:            Implement JsonSerializable interface in stdClass
 Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Class/Object related
 PHP Version:        7.4.9
 Block user comment: N
 Private report:     N

 New Comment:

I think it's worth mentioning here that using JsonSerializable as a type hint makes very little
sense. The JsonSerializable class is a bit of a misnomer, and should really be something like
HasCustomJsonSerializationLogic. Your default assumption should be that *everything* is JSON
serializable.


Previous Comments:
------------------------------------------------------------------------
[2020-08-13 13:23:35] brzuchal@php.net

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions.

The stdClass is a class which don't implement any interface on purpose
cause it has no behaviour. Whenever you cast to an (object) or use
json_decode() you get stdClass objects initially with no behaviour as well.

Thank you for your interest in PHP.

------------------------------------------------------------------------
[2020-08-13 13:15:41] fodor dot laszlo at gmail dot com

Description:
------------
It would be great if stdClass would implement JsonSerializable for multiple reasons.
First of all, since the interface has been introduced a lot of methods are expecting
JsonSerializable as a parameter to work with by type-hinting the parameter.
And sometimes these methods should work properly with an array as well.
If stdClass would implement JsonSerializable interface a simple (object) casting would be enough to
pass an array to such method.

Another reason is testing. Currently I need to set up a Mock object to test a method what expects a
JsonSerializable. Instead I could just use a simple stdClass with some dummy data in it



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79972&edit=1


Thread (7 messages)

« previous php.bugs (#228576) next »