Rofihimam

Untitled

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