|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65858 json_encode could consider __get magic method
Submitted: 2013-10-08 15:10 UTC Modified: 2013-10-08 15:13 UTC
From: leonardo at ebussola dot com Assigned:
Status: Wont fix Package: JSON related
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: leonardo at ebussola dot com
New email:
PHP Version: OS:


 [2013-10-08 15:10 UTC] leonardo at ebussola dot com
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:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-08 15:13 UTC]
-Status: Open +Status: Wont fix
 [2013-10-08 15:13 UTC]
__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.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 22 22:01:28 2024 UTC