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
 [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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC