Jump to content

[SOLVED] Noob question about class methods


ballhogjoni

Recommended Posts

my understanding of it, is its similar to variable scope in a way, I think Private functions can only be used within the class its declared in, and public functions can be used in the class its defined in, as well as classes that extend it etc...

 

however in saying that, I'm not entirely sure.

They do what their english meanings imply: public functions/variables are available outside and inside the class. Private functions/variables are available only inside the class. Protected ones, on the other hand, are available only inside the class, and in the classes that extend it.

 

Example:

class Foo
{
      public function foo() 
      {
           // these will work
           $this->foo();
           $this->bar();
           $this->boo();
      }
      protected function boo() { ... }
      private function bar() { ... }
}

class Bar extends Foo
{
      public function hello()
      { 
           $this->foo();    // works
           $this->boo();   // works
           $this->bar();    // does not work
      }
}

$ex = new Foo();
$ex->foo();    // work
$ex->boo();    // does not work
$ex->bar();    // does not work

 

Hopefully that makes sense.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.