<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>
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces object-oriented PHP (3), including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.