You see this title may first think of is __get magic method, Nest also thought, but--
Because I will need to use Json_encode ($obj) to output to JS access, so here the some_attr can only be public.
Then you can not use __get to do it when calling $obj->some_attr ...
Is there anything more "magic" a bit of a way ...?
I really do not want to get a $obj->to_json () This kind of thing ...
Reply content:
You see this title may first think of is __get magic method, Nest also thought, but--
Because I will need to use Json_encode ($obj) to output to JS access, so here the some_attr can only be public.
Then you can not use __get to do it when calling $obj->some_attr ...
Is there anything more "magic" a bit of a way ...?
I really do not want to get a $obj->to_json () This kind of thing ...
You mean to specify which members are to be serialized within the class:
1) Let your class to implement the Serializable interface (5.1 support), write a Serialize method to return the data json_encode;
2) or to implement the Jsonserializable interface (5.4+).
$name (); } protected function _var_a () { $this->var_a = ' a '; return $this->var_a; }} $test = new Test (), $test->_var_a; $str = Json_encode ($test); Var_dump ($STR);
This is what the demand. Or did I get the wrong idea?
Direct external operation not on the line, why so complex.