php类的继承,重载方法

php类的继承,重载方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
//类
class ParentClass{
    function show_message(){
        echo "\n","\n";
        echo "Hi!This comes from Parent class.";
    }
}

//类的继承
class ChildClass extends ParentClass{
    //重载ParentClass的show_message()函数
    function show_message()
    {
        echo "\n","\n";
        echo "Hi!This comes from the child class.";
    }
    //调用ParentClass的show_message()函数
    function show()
    {
        parent::show_message();
    }
}
$obj2 = new ChildClass; //ChildClass的对象
$obj2 -> show();    //这将显示父类的show_message()中定义的消息
?>