Home >Backend Development >PHP Tutorial >47 PHP magic methods

47 PHP magic methods

WBOY
WBOYOriginal
2016-07-29 09:13:31784browse

1. __toString()
47 <strong>PHP magic method</strong>

2. __invoke()
47 <strong>PHP magic method</strong>

3. call(), callStatic() — Method overloading (overwrite)
47 <strong>PHP magic method</strong>

47 <strong>PHP magic method</strong>

4. __get(), __set() —Attribute overloading (overloading)
47 <strong>PHP Magic Method</strong>

5. __clone()

47 <strong>PHP Magic Method</strong>

<code><span><span>class</span><span>A</span>
{</span><span>public</span><span>$name</span>;
}

<span>$aa</span> = <span>new</span> A();
<span>$aa</span>->name = <span>'wjh'</span> . <span>'<br/>'</span>;
<span>echo</span><span>$aa</span>->name;

<span>$bb</span> = <span>clone</span><span>$aa</span>;
<span>$bb</span>->name = <span>'wang lu xi'</span>;  <span>//不改变赋值对象的属性</span><span>echo</span><span>$bb</span>->name . <span>'<br/>'</span>;
<span>echo</span><span>$aa</span>->name . <span>'<br/>'</span>;</code>
<code><span><span><?php</span><span><span>class</span><span>A</span>
{</span><span>public</span><span>$name</span>;

    <span>public</span><span><span>function</span><span>__clone</span><span>()</span>  // 魔术方法,只要<span>clone</span>就运行这个方法
    {</span><span>$this</span>->name = <span>' wei jian hua'</span>;
    }
}

<span>$aa</span> = <span>new</span> A();
<span>$aa</span>->name = <span>'wjh'</span> . <span>'<br/>'</span>;
<span>echo</span><span>$aa</span>->name;

<span>$bb</span> = <span>clone</span><span>$aa</span>;

<span>echo</span><span>$bb</span>->name;</span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces 47 PHP magic methods, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn