Oop-PHP: How to automatically call $ obj-> some_attr () before calling $ obj-> some_func ()

Source: Internet
Author: User
You may think of the _ get magic method as the title first, but also the nest, but -- because I will need to use json_encode ($ obj) to output to JS access, so some_attr here can only be public. When $ obj-& amp; gt; some_attr is called, _ get cannot be used for this... you may think of the _ get magic method first when you see this title --

Because I will use json_encode ($ obj) to output to JS access, some_attr here can only be public.
When you call $ obj-> some_attr, you cannot use _ get...

Is there any more "magic" method ..?
I really don't want to get something like $ obj-> to_json...

Reply content:

You may think of the _ get magic method first when you see this title --

Because I will use json_encode ($ obj) to output to JS access, some_attr here can only be public.
When you call $ obj-> some_attr, you cannot use _ get...

Is there any more "magic" method ..?
I really don't want to get something like $ obj-> to_json...

You mean to specify the Members to be serialized within the class:

1) Let your class implement the Serializable interface (5.1 is supported), and write a serialize method to return json_encode data;

2) or 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);

What is this requirement. Or am I wrong?
Direct external operations are not enough. Why bother.

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.