oop-php: How to Call $obj->some_func () automatically before calling $obj->some_attr

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.