Req #60924 [Fbk->Asn]: ArrayObject should implement JsonSerializable interface
Edit report at https://bugs.php.net/bug.php?id=60924&edit=1
ID: 60924
User updated by: chris at cmbuckley dot co dot uk
Reported by: chris at cmbuckley dot co dot uk
Summary: ArrayObject should implement JsonSerializable
interface
-Status: Feedback
+Status: Assigned
Type: Feature/Change Request
Package: SPL related
PHP Version: 5.4.0RC6
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Casting my mind back to the intention 8 years ago, I believe the idea was flawed. I think my thought
was something along the lines of "if something can be JSON-serialised, it should implement
JsonSerializable". Though as you say, all PHP objects can be JSON-serialised, so application
code should transparently use json_encode($obj) and a class should only use
JsonSerializable if it should customise the JSON output.
Feel free to close the ticket.
Previous Comments:
------------------------------------------------------------------------
[2020-12-07 15:41:04] cmb@php.net
Being able to be json_encode()d, is the standard behavior of PHP
objects. Why should ArrayObject implement JsonSerializable?
------------------------------------------------------------------------
[2012-01-29 17:45:37] chris at cmbuckley dot co dot uk
Description:
------------
PHP 5.4 brings the new JsonSerializable interface. ArrayObject already works with json_encode as
expected but does not explicitly implement this interface.
Test script:
---------------
$ao = new ArrayObject(array('foo' => 'bar'));
var_dump(json_encode($ao));
var_dump($ao instanceof JsonSerializable);
var_dump($ao->jsonSerialize());
Expected result:
----------------
string(13) "{"foo":"bar"}"
bool(true)
string(13) "{"foo":"bar"}"
Actual result:
--------------
string(13) "{"foo":"bar"}"
bool(false)
PHP Fatal error: Call to undefined method ArrayObject::jsonSerialize() in - on line 6
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60924&edit=1
Thread (4 messages)