Home  >  Article  >  Backend Development  >  PHP class definition and inheritance usage examples

PHP class definition and inheritance usage examples

WBOY
WBOYOriginal
2016-07-25 08:44:50856browse

The examples in this article describe the definition and inheritance usage of PHP classes. Share it with everyone for your reference. The details are as follows:

  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 "Name:".$this->name."
    "."Age: ".$this->age;
  13. }
  14. function __tostring() {
  15. return "Name: ".$this->name."
    "."Age: ".$this-> age;
  16. }
  17. function __destruct() {
  18. echo "
    I am dead!";
  19. }
  20. function __call($key,$args) {
  21. echo "
    "," The method name you called does not exist: $key","
    ";
  22. echo "The parameters you called are:",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 = "Xiao Ming";
  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. ?>
Copy code

I hope this article will be helpful to everyone’s PHP programming design.

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn