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
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: 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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC