Home  >  Article  >  Backend Development  >  Object-oriented PHP (2)

Object-oriented PHP (2)

WBOY
WBOYOriginal
2016-07-29 09:16:21915browse

<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>

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 (2), including aspects of 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