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 this is not your bug, you can add a comment by following this link.
If this is your bug, but 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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 19 17:01:30 2024 UTC