Req #65858 [Opn->Wfx]: json_encode could consider __get magic method
| From: | nikic@php.net | Date: | Tue, 08 Oct 2013 15:13:53 +0000 |
| Subject: | Req #65858 [Opn->Wfx]: json_encode could consider __get magic method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182089@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65858&edit=1
ID: 65858
Updated by: nikic@php.net
Reported by: leonardo at ebussola dot com
Summary: json_encode could consider __get magic method
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: JSON related
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
__get is invoked when an inaccessible property is accessed - not the case here (as $foo is public).
If you want to customize json serialization make use of the JsonSerializable interface.
Previous Comments:
------------------------------------------------------------------------
[2013-10-08 15:10:35] leonardo at ebussola dot com
Description:
------------
When I use json_encode on a class, this get the properties values directly and bypass the __get
magic method.
It would be great if I could control the properties with __get
Test script:
---------------
class Example {
public foo;
public function __construct() {
$this->foo = 'bar;
}
public function __get($param) {
return $this->$param . ' modified';
}
}
$foo = new Example();
echo json_encode($foo);
Expected result:
----------------
{"foo":"bar modified"}
Actual result:
--------------
{"foo":"bar"}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65858&edit=1