<code><span><span><?php</span>header(<span>"Content-Type: text/html; charset=utf-8"</span>);<span>//去除中文乱码</span><span>/** * 1.parent关键字可以用于调用父类中被重写了的方法 * 2.self关键字可以用于访问类自身的成员方法、静态方法和类常量,自身属性不能被访问;使用常量的时候,常量名称不需要加$ * 3.static关键字用于访问类自身定义的静态成员,访问静态属性时需要在属性前加$ */</span><span><span>class</span><span>BaseClass</span>{</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>const</span> C>"类中定义的一个const常量"</span>; <span>private</span><span>static</span><span>$staticValue</span>=<span>"这是类的static常量"</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>echo</span><span>parent</span>::test();<span>//调用被重写的父类方法</span><span>self</span>::called(); <span>echo</span><span>self</span>::CONST_VALUE.<span>"<br/>"</span>; <span>echo</span><span>self</span>::<span>$staticValue</span>.<span>"<br/>"</span>; } <span>public</span><span><span>function</span><span>called</span><span>()</span>{</span><span>echo</span><span>"类的一个方法"</span>.<span>"<br/>"</span>; } } <span>$obj</span>=<span>new</span> ChildClass(); <span>$obj</span>->test(<span>"'222'"</span>); <span>//$obj->test1();</span><span>?></span></code>
저작권: 이 글은 블로거의 원본 글이므로 블로거의 허락 없이 복제할 수 없습니다.
위의 내용은 객체지향 PHP(3)에 대한 내용을 포함하여 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.