You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.8 KiB

3 years ago
  1. <?php
  2. //继承父类中的publc成员demo
  3. class classParent
  4. {
  5. public function __construct()
  6. {
  7. echo '--进入父类--'.'<br>';
  8. }
  9. public function demo() //public 成员
  10. {
  11. echo '--父类 方法--'.'<br>';
  12. }
  13. public function __destruct()
  14. {
  15. echo '--推出父类--'.'<br>';
  16. }
  17. }
  18. class son extends classParent
  19. {
  20. public function __construct()
  21. {
  22. echo '--进入子类--'.'<br>';
  23. }
  24. public function __destruct()
  25. {
  26. echo '--退出子类--'.'<br>';
  27. }
  28. }
  29. // $obj = new son();
  30. // $obj -> demo();
  31. /*继承父类中的保存成员*/
  32. class parent2
  33. {
  34. public function __construct()
  35. {
  36. echo '--进入父类--'.'<br>';
  37. }
  38. protected function demo()
  39. {
  40. echo '父类保护成员'.'<br>';
  41. }
  42. public function __destruct()
  43. {
  44. echo '--退出父类--'.'<br>';
  45. }
  46. }
  47. class son2 extends parent2{
  48. public function __construct()
  49. {
  50. echo '--进入子类--'.'<br>';
  51. }
  52. public function test()
  53. {
  54. echo '子类公共成员'.'<br>';
  55. $this -> demo(); //这个this是指子son2;
  56. }
  57. public function __destruct()
  58. {
  59. echo '--退出子类--'.'<br>';
  60. }
  61. }
  62. // $obj = new son2();
  63. // $obj -> test();
  64. /*继承父类中的私有成员*/
  65. class parent3{
  66. private function func(){
  67. echo '父类私有成员'.'<br>';
  68. }
  69. }
  70. class son3 extends parent3{
  71. public function test(){
  72. $this -> func(); //会报错
  73. }
  74. }
  75. $obj=new son3();
  76. $obj -> test();
  77. ?>