php类的定义与继承用法实例

时间:2021-11-05 09:21:32

本文实例讲述了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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/*
 * class
 */
 class people {
  public $name;
  public $age;
  function __construct($namec,$agec) {
    $this->name = $namec;
    $this->age = $agec;
  }
  protected function getmessage() {
    return "姓名:".$this->name."<br/>"."年龄:".$this->age;
  }
  function __tostring() {
    return "姓名:".$this->name."<br/>"."年龄:".$this->age;
  }
  function __destruct() {
    echo "<br/> I am dead!";
  }
  function __call($key,$args) {
    echo "<br/>","你调用的方法名不存在:$key","<br/>";
    echo "你调用的参数是:",var_dump($args);
  }
  final function getf() {
    echo "I am getf";
  }
 }
 class xinxin extends people {
  function getname() {
    echo $this->getmessage();
    echo '<br/>';
    echo parent::getmessage();
    echo '<br/>';
    return "I am xinxin";
  }
  function getmessage() {
    return "I am zilei getmessage <br/>";
  }
  function getff() {
    echo "I am new getf";
  }
 }
$pp = new people("小弟","33");
//$pp->name = "小明";
//$pp->age = "88";
echo $pp->name;
echo '   ';
echo $pp->age;
echo '<br/><br/>';
$xx = new xinxin("小小","13");
echo $xx->getname();
?>

希望本文所述对大家的php程序设计有所帮助。