PHP Common Magic Method (__set, __get Magic method:)

Source: Internet
Author: User
<span id="Label3"></p><p><p>__set, __get Magic Method:</p></p><pre><pre></pre></pre><pre><pre><span style="color: #000080; font-weight: bold;"><span style="color: #000080; font-weight: bold;"><span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">file name:<span style="color: #808080; font-style: italic;">object.php</span> </span></span></span></span></pre></pre><pre><span style="color: #000080; font-weight: bold;"><?php<br><span style="color: #000080; font-weight: bold;">Namespace<span>imooc;<span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"><span style="color: #808080; font-style: italic;"><br><span style="color: #000080; font-weight: bold;">Class<span>Object<br><span>{<br><span> <span style="color: #000080; font-weight: bold;">Protected<span style="color: #660e7a; font-weight: bold;">$array<span>=<span style="color: #000080; font-weight: bold;">Array<span>();<br><span> <span style="color: #000080; font-weight: bold;">function<span>__set (<span style="color: #660000;">$name<span>,<span style="color: #660000;">$value<span>)<span style="color: #808080; font-style: italic;">Set<span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">Magic method<br><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"> <span>{<br><span> <span style="font-style: italic;">Var_dump<span>(<span style="color: #660e7a; font-weight: bold; font-style: italic;">__method__<span>);<span style="color: #808080; font-style: italic;">//<span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">Output Current method<br><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"><br><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"> <span style="color: #660000;">$this<span>-<span style="color: #660e7a; font-weight: bold;">Array<span>[<span style="color: #660000;">$name<span>]=<span style="color: #660000;">$value<span>;<span style="color: #808080; font-style: italic;">$name =title, $value = "<span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">ouch, Whoa.<span style="color: #808080; font-style: italic;">"<br><span style="color: #808080; font-style: italic;"> <span>}<br><span> <span style="color: #000080; font-weight: bold;">function<span>__get (<span style="color: #660000;"><span style="color: #660000;"> $name <span>) <span style="color: #808080; font-style:italic; >//get<span style=" color: #808080; font-style:italic; font-family: ' the song body ';> Magic method <br> <span style="color: #808080; font-style:italic; font-family: ' Arial ';"> <span> {<br> <span> <span style=" font-style:italic; ">var_dump <span> (<span style="color: #660e7a; font-weight:bold; font-style:italic;">__method__ <span>); <span style="color: #808080; font-style:italic; ">//<span style="color: #808080; font-style:italic; font-family: ' Arial ';"> output current method <br> <span style="color: #808080; font-style:italic; Font-family: ' The song Body '; > <span style= " color: #000080; font-weight:bold;">return <span style="color: #660000;"> $this <span>-><span style="color: #660e7a; font-weight:bold; ">array <span> [<span style="color: #660000;"> $name <span>]; <br> <span>} <br> <span>} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> /span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><pre><pre><span style="color: #000000;"><br><br></span></pre></pre><pre><pre><span style="color: #000080; font-weight: bold;"><span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">file name:<span style="color: #808080; font-style: italic;">index.php</span> </span></span></span></pre></pre><pre><span style="color: #000080; font-weight: bold;"><?php<br><span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"><span style="color: #808080; font-style: italic;"><br><span style="font-style: italic;">Define<span>(<span style="color: #008000; font-weight: bold;">A<span>,<span style="color: #660e7a; font-weight: bold; font-style: italic;">__dir__<span>);<br><span style="color: #000080; font-weight: bold;">Include<span style="color: #008000; font-weight: bold;">'/imooc/loader.php '<span>;<br><span>Spl_autoload_register (<span style="color: #008000; font-weight: bold;">‘<span style="color: #000080; font-weight: bold;">\\<span style="color: #008000; font-weight: bold;">Imooc<span style="color: #000080; font-weight: bold;">\\<span style="color: #008000; font-weight: bold;">Loader::autoload '<span>);<br><span><br><span><br><span style="color: #660000;">$Object<span>=<span style="color: #000080; font-weight: bold;">New<span>\imooc\object ();<br><span><br><span style="color: #660000;">$Object<span>-<span style="color: #660e7a; font-weight: bold;">Title<span>=<span style="color: #008000; font-weight: bold;">"<span style="color: #008000; font-weight: bold; font-family: ‘宋体‘;">ouch, Whoa.<span style="color: #008000; font-weight: bold;">"<span>;<span style="color: #808080; font-style: italic;">//<span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">When an assignment is made to a non-existent property, it invokes the<span style="color: #808080; font-style: italic;">__set<span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">Magic method<br><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"><br><span style="color: #000080; font-weight: bold;">Echo<span style="color: #660000;">$Object<span>-<span style="color: #660e7a; font-weight: bold;">Title<span><span>; <span style="color: #808080; font-style: italic;">// <span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"> <span style="color: #808080; font-style: italic;">__get Magic method is called when the Read object does not have a property <span style="color: #808080; font-style: italic; font-family: ‘宋体‘;"><br><span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">/* output:<span style="color: #808080; font-style: italic;"><br><span style="color: #808080; font-style: italic;">String ("imooc\object::__set")<br><span style="color: #808080; font-style: italic;">String ("imooc\object::__get")<br><span style="color: #808080; font-style: italic;"><span style="color: #808080; font-style: italic; font-family: ‘宋体‘;">ouch, Whoa.<span style="color: #808080; font-style: italic;"><br><span style="color: #808080; font-style: italic;">*/<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p></p></p><p><p> Php Common Magic method (__set, __get Magic method:) </p> </p></span>

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.