php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45375 __object_vars() magic method
Submitted: 2008-06-27 09:15 UTC Modified: 2010-09-16 16:03 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: margus dot sipria at gmail dot com Assigned: aharvey (profile)
Status: Closed Package: JSON related
PHP Version: 5.2.6 OS: *
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: margus dot sipria at gmail dot com
New email:
PHP Version: OS:

 

 [2008-06-27 09:15 UTC] margus dot sipria at gmail dot com
Description:
------------
When I use __set and __get methods on object i have set data to my object, and when i wanna get thows methods i would only get public methods that are non.

Same think is happened when i wanna example transfere Object to json format, there should be some way that i could get data that i wanna.

This could be very useful example using with Zend Framwork encoding a Database Table Row (http://framework.zend.com/manual/en/zend.db.table.row.html) where data is in private variable.

if not a Core function (that it should be in my opinion) then SPL interface would be needed.

Reproduce code:
---------------
class Test {
    protected $_data = array();
    public function __set($var, $value) {$this->_data[$var] = $value;}    
    public function __get($var) {
        if (isset($this->_data[$var])) {return $this->_data[$var]; }
        trigger_error('Undefined property: ' . __CLASS__ . '::$' . $var, E_USER_NOTICE);
        return null;
    }
    public function __isset($var) {return isset($this->_data[$var]);}
    
    public function __object_vars() {
        return $this->_data;
    }
}
$json = new Test();
$json->variable = 'value';

echo json_encode($json);

Expected result:
----------------
{"variable":"value"}

Actual result:
--------------
{}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-16 16:03 UTC] aharvey@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: JSON related -Assigned To: +Assigned To: aharvey
 [2010-09-16 16:03 UTC] aharvey@php.net
Trunk now has the JsonSerializable interface which implements equivalent functionality, just with a slightly different API.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Oct 24 19:01:23 2020 UTC