|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-12-18 18:24 UTC] dthomas at starkartthenala dot com
[2014-12-18 18:51 UTC] aharvey@php.net
-Summary: Encoding a clsoure as "{}" in json_encode
+Summary: Closure should implement JsonSerializable
-Type: Bug
+Type: Feature/Change Request
-Package: JSON related
+Package: Scripting Engine problem
[2014-12-18 18:51 UTC] aharvey@php.net
[2017-08-05 04:54 UTC] stas@php.net
-Status: Open
+Status: Suspended
[2017-08-05 04:54 UTC] stas@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 15:00:02 2025 UTC |
Description: ------------ The `json_encode` in PHP 5.5.17 encodes a closure object as "{}". By analyzing the code, I found out that it might be a bug, since json encoder doesn't have any special treatment of closure objects, and just fallback to the array-encoder, which eventually outputs the "{}". Example: // PHP json_encode([1,2,3, function () {}]); // "[1,2,3,{}]" In HHVM team we'd like to conform the behavior (the discussion is in this issue thread: https://github.com/facebook/hhvm/issues/4035), however, think that encoding a closure as "{}" doesn't make big sense, since it cannot be decoded back as a closure. For the record, e.g. ECMAScript encodes all non-JSON values as undefined (or null in arrays): // ECMAScript JSON.stringify([1,2,3, function () {}]) // "[1,2,3,null]" My question are: 1. Is it actual PHP bug? 2. If yes, will it make sense to encode closure objects as `null` instead of "{}"? Dmitry Test script: --------------- <?php json_encode([1,2,3, function () {}]); // "[1,2,3,{}]" Expected result: ---------------- "[1,2,3,null]" Actual result: -------------- "[1,2,3,{}]"