Advertisement
sri211500

Untitled

Feb 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. <?php
  2. /**
  3. **Define MyClass
  4. */
  5. class MyClass
  6. {
  7. // Declare a public constructor
  8. public function __construct() {}
  9.  
  10. // Declare a public method
  11. public function MyPublic() {}
  12.  
  13. // Declare a proctected method
  14. proctected function Myproctected() {}
  15.  
  16. // declare a private method
  17. private function MyPrivate() {}
  18.  
  19. // This is public
  20. function Foo()
  21. {
  22. $this->MyPublic();
  23. $this->Myproctected();
  24. $this->MyPrivate();
  25. }
  26. }
  27.  
  28. $myclass = new MyClass;
  29. $myclass->MyPublic();//works
  30. $myclass->Myproctected();// Fatal Error
  31. $myclass->MyPrivate();// Fatal Error
  32. $myclass->Foo();// Public, Protected and Private work
  33.  
  34.  
  35. /**
  36. * Define MyClass
  37. */
  38. class MyClass2 extends MyClass
  39. {
  40. // This is public
  41. function Food2()
  42. {
  43. $this->MyPublic();
  44. $this->Myproctected();
  45. $this->MyPrivate();// Fatal Error
  46. }
  47. }
  48.  
  49. $myclass = new MyClass2;
  50. $myclass->MyPublic();// works
  51. $myclass->Foo2();// Public and Protected work, not Private
  52.  
  53. class Bar
  54. {
  55. public function test() {
  56. $this->testPrivate();
  57. $this->testPublic();
  58. }
  59.  
  60. public function testPublic() {
  61. echo "bar::testPublic\n";
  62. }
  63.  
  64. private function testPrivate() {
  65. echo "Bar::testPrivate\n";
  66. }
  67. }
  68.  
  69. class Foo extends Bar
  70. {
  71. public function testPublic() {
  72. echo "Foo::testPublic\n";
  73. }
  74.  
  75. private function testPrivate() {
  76. echo "Foo::testPrivate\n";
  77. }
  78. }
  79.  
  80. $myFoo = new Foo();
  81. $myFoo->test();// Bar::testPrivate
  82. $myFoo->test();// Foo::testPublic
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement