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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.