Method Visibility Class.


Method Visibility  Class methods must be defined with public, private, or protected. Methods without any declaration are defined as public.  // Define MyClass class MyClass {   // Contructors must be public   public 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 by default   function Foo() {     $this->MyPublic();     $this->MyProtected();     $this->MyPrivate();     } } // MyClass  $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 by default   function Foo2() {     $this->MyPublic();     $this->MyProtected();     $this->MyPrivate();   // Fatal Error     } } // MyClass2  $myclass2 = new MyClass2; $myclass2->MyPublic(); // Works! $myclass2->Foo2();     // Public and Protected work, not Private.

Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

1 comments: