对象向下传递特性
当一个对象调用一个实例方法,然后再该方法中又去静态调用另一个类的方法,则在被静态调用的方法中获得源方法中的对象(this)
<code><span><span><?php </span><span><span>class</span><span>bee</span>{</span><span>public</span><span>$a</span> = <span>1</span>; <span>public</span><span><span>function</span><span>f</span><span>()</span>{</span><span>echo</span><span>$this</span>->a; <span>echo</span><span>'<br>'</span>; @lig::f(); } } <span><span>class</span><span>lig</span>{</span><span>public</span><span>$a</span> = <span>2</span>; <span>public</span><span><span>function</span><span>f</span><span>()</span>{</span><span>echo</span><span>$this</span>->a; } } <span>$obj</span> = <span>new</span> bee(); <span>$obj</span>->f();</span></span></code>
static 后期静态绑定
static有3个不同含义的语法
<code><span><span>function</span><span>f</span><span>()</span>{</span><span>static</span><span>$v</span> = <span>1</span>; }</code>
<code><span><span>class</span><span>bee</span>{</span><span>static</span><span>$v</span> = <span>1</span>; <span>static</span><span><span>function</span><span>f</span><span>()</span>{</span>} }</code>
<code><span><span>class</span><span>bee</span>{</span><span>static</span><span>public</span><span>$a</span> = <span>10</span>; <span>static</span><span>public</span><span><span>function</span><span>f</span><span>()</span>{</span><span>echo</span> get_class().<span>':'</span>; <span>echo</span><span>self</span>::<span>$a</span>.<span>'-'</span>; <span>echo</span><span>static</span>::<span>$a</span>; } } <span><span>class</span><span>lig</span><span>extends</span><span>bee</span>{</span><span>static</span><span>public</span><span>$a</span> = <span>20</span>; } <span>echo</span> bee::f(); <span>echo</span><span>'<br>'</span>; <span>echo</span> lig::f();</code>
面向对象三大思想
以上就介绍了PHP之类特性,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。