当前位置:Gxlcms > PHP教程 > php类的定义与继承用法实例

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

时间:2021-07-01 10:21:17 帮助过:20人阅读

本文实例讲述了php类的定义与继承用法。分享给大家供大家参考。具体如下:

  1. /*
  2. * class
  3. */
  4. class people {
  5. public $name;
  6. public $age;
  7. function __construct($namec,$agec) {
  8. $this->name = $namec;
  9. $this->age = $agec;
  10. }
  11. protected function getmessage() {
  12. return "姓名:".$this->name."
    "."年龄:".$this->age;
  13. }
  14. function __tostring() {
  15. return "姓名:".$this->name."
    "."年龄:".$this->age;
  16. }
  17. function __destruct() {
  18. echo "
    I am dead!";
  19. }
  20. function __call($key,$args) {
  21. echo "
    ","你调用的方法名不存在:$key","
    ";
  22. echo "你调用的参数是:",var_dump($args);
  23. }
  24. final function getf() {
  25. echo "I am getf";
  26. }
  27. }
  28. class xinxin extends people {
  29. function getname() {
  30. echo $this->getmessage();
  31. echo '
    ';
  32. echo parent::getmessage();
  33. echo '
    ';
  34. return "I am xinxin";
  35. }
  36. function getmessage() {
  37. return "I am zilei getmessage
    ";
  38. }
  39. function getff() {
  40. echo "I am new getf";
  41. }
  42. }
  43. $pp = new people("小弟","33");
  44. //$pp->name = "小明";
  45. //$pp->age = "88";
  46. echo $pp->name;
  47. echo ' ';
  48. echo $pp->age;
  49. echo '

    ';
  50. $xx = new xinxin("小小","13");
  51. echo $xx->getname();
  52. ?>

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

php

人气教程排行