<code><span><span><?php</span><span>/** * 1.子类与父类方法相同可以对父类方法重写 * 2.如果不希望父类被重写,用final关键字修饰,可以修饰类中方法,也可以是一个类(该类所有方法均不能被重写) */</span>header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span><span>final</span><span><span>class</span><span>BaseClass</span>{</span><span>final</span><span>public</span><span><span>function</span><span>test</span><span>()</span>{</span><span>echo</span><span>"BaseClass::test called.<br/>"</span>; } <span>public</span><span><span>function</span><span>test1</span><span>()</span>{</span><span>echo</span><span>"BaseClass::test1 called.<br/>"</span>; } } <span><span>class</span><span>ChildClass</span><span>extends</span><span>BaseClass</span> {</span><span>public</span><span><span>function</span><span>test</span><span>(<span>$value</span>=null)</span>{</span><span>//没有默认值会报错</span><span>echo</span><span>"ChildClass::test called "</span>.<span>$value</span>.<span>"<br/>"</span>; } } <span>$obj</span>=<span>new</span> ChildClass(); <span>$obj</span>->test(<span>"'222'"</span>); <span>$obj</span>->test1(); <span>?></span></span></code>
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了物件導向PHP(二),包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。