Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Define MyClass
- */
- class MyClass
- {
- // Declare a public constructor
- function __construct() {}
- // Declare a public method
- public function MyPublic()
- {
- }
- // Declare a protected method
- protected function MyProtected()
- {
- }
- // Declare a private method
- private function MyPrivate()
- {
- }
- // This is public
- function Foo()
- {
- $this->MyPublic();
- $this->MyProtected();
- $this->MyPrivate();
- }
- }
- $myclass = new MyClass;
- $myclass->MyPublic(); // works
- $myclass->MyProtected(); // Fatal error
- $myclass->MyPrivate(); // Fatal error
- $myclass->Foo(); // Public, protected and private work
- /**
- * define MyClass2
- */
- class MyClass2 extends MyClass
- {
- // This is public
- function Foo2()
- {
- $this->MyPublic();
- $this->MyProtected();
- $this->MyPrivate(); // fatal error
- }
- }
- $myclass2 = new MyClass2;
- $myclass2->MyPublic(); // works
- $myclass2->Foo2(); // public and protected work, not private
- class Bar
- {
- public function test()
- {
- $this->testPrivate();
- $this->testPublic();
- }
- public function testPublic()
- {
- echo "Bar::testPublic\n";
- }
- public function testPrivate()
- {
- echo "Bar::testPrivate\n";
- }
- }
- class Foo extends Bar
- {
- public function testPublic()
- {
- echo "Foo::testPublic\n";
- }
- public function testPrivate()
- {
- echo "Foo::testPrivate\n";
- }
- }
- $myFoo = new Foo();
- $myFoo->test(); // Bar::testPrivate
- // Foo::testPublic
- ?>
Advertisement
Add Comment
Please, Sign In to add comment