?>
<?php
class father{
protected $name;
function __construct($name){
$this->name=$name;
}
function __destruct(){
echo "<p>{$this->name}也是要死的<br/></p>";
}
//
这个就是所谓的构造函数,用来初始化
function go_to_sleeping(){
echo "<p>{$this->name}想睡觉.</p>";
}
function eat(){
echo "<p>{$this->name}想吃饭.</p>";
}
}
class son extends father{
function playing(){
//小孩子会很调皮的,
当然他也是要吃要睡的生物
echo "<p>{$this->name}正在捣蛋...</p>";
}
}
$your_father=new father("老爸");
$your_father->go_to_sleeping();
$your_father->eat();
$my_son=new son('宝贝');
$my_son->go_to_sleeping();
$my_son->eat();
$my_son->playing();
?>
解析:在我们的第一个使用继承的范例里使用了 PHP 的构造函数中所提到的构造函数和
PHP 类的封装中的关键字,如果有什么不明白的就去看看吧!我可不想再多说了,中午
没想睡觉呀.说说这段小程序吧。
在类的 father 里,我们定义了一般的特征,比如人的名字,人要吃饭和睡觉,然后在它
的子类(后代)我们定义一个个性化的方法 (playing),毕竟人与人之间是有不同的地方
“
”
吧。我们使用构造函数来初始化这个人名,当然也使用了析构函数来 消灭 掉对象,但你
可能没有发现在子类里内是没有构造函数和析构函数的,所以子类是继承了父的所有方
法,要不然你怎么能$my_son->go_to_sleeping();这样调用呢,这就是 PHP 类的继承了。
文章引用自:http://club.topsage.com/thread-501298-1-1.html
<?php
class my_class{
var $username="小鸡";