php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: leonardo at ebussola dot com
New email:
PHP Version: OS:

 

 [2013-10-08 15:10 UTC] 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"}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-08 15:13 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-10-08 15:13 UTC] nikic@php.net
__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-2025 The PHP Group
All rights reserved.
Last updated: Tue Jan 07 21:01:29 2025 UTC