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

这篇文章主要介绍了php类的定义与继承用法,实例分析了php中类的基本定义与继承的使用技巧,需要的朋友可以参考下,本文实例讲述了php类的定义与继承用法,分享给大家供大家参考,具体如下:

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