Jump to content

Recommended Posts

Private methods can only be called from the internals of the class. Protected methods can only be called from the internals of the class or any derived classes (sub-classes). Public methods can be called from anywhere.

 

Without specifying anything a method or function is deemed public.

 

Do some Googling on the topic for examples.

Link to comment
https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660055
Share on other sites

When you're working in OOP and you have a private function, what exactly is "private" about it?

 

It can't be invoked by the client code.  Example:

class PrivateExample
{
   private $msg = "Hello!";

   public function __construct() {}

   private function getMsg()
   {
      return $this->msg;
   }
}

$example = new PrivateExample();
$msg = $example->getMsg(); // WON'T WORK

 

Private functions are typically used as bookkeeping functions for the objects in which they reside.  That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large.

Link to comment
https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660057
Share on other sites

When you're working in OOP and you have a private function, what exactly is "private" about it?

 

It can't be invoked by the client code.  Example:

class PrivateExample
{
   private $msg = "Hello!";

   public function __construct() {}

   private function getMsg()
   {
      return $this->msg;
   }
}

$example = new PrivateExample();
$msg = $example->getMsg(); // WON'T WORK

 

Private functions are typically used as bookkeeping functions for the objects in which they reside.  That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large.

 

OK I think I'm following you on this.

 

In other words, private functions are functions only someone running Administrator codes can access - meaning, if I'm logged into my admin panel, then I have access to those Private functions?

 

er am I off base here?

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660071
Share on other sites

When you're working in OOP and you have a private function, what exactly is "private" about it?

 

It can't be invoked by the client code.  Example:

class PrivateExample
{
   private $msg = "Hello!";

   public function __construct() {}

   private function getMsg()
   {
      return $this->msg;
   }
}

$example = new PrivateExample();
$msg = $example->getMsg(); // WON'T WORK

 

Private functions are typically used as bookkeeping functions for the objects in which they reside.  That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large.

 

OK I think I'm following you on this.

 

In other words, private functions are functions only someone running Administrator codes can access - meaning, if I'm logged into my admin panel, then I have access to those Private functions?

 

er am I off base here?

Thanks :)

 

Way off base. :P

 

It has nothing to do with site administration or authorization.  We should probably start at the beginning.

 

Objects are datastructures, much like arrays or structs (or even functions).  No more, no less.  They are defined by classes, which, really, is another word for datatype.  So, let's define a class:

class Example
{
   public $msg = "Hi, I'm an object property!"

   public function __construct()
   {
      echo "I'm the object constructor.  I run every time a new Example object is instantiated!";
   }
}

 

Pretty straightforward.  It has one property ($msg) and one method, its constructor.  Now, let's create a few of these objects in our client code:

$example1 = new Example();
$example2 = new Example();

 

Again, simple.  Now, what if you wanted to access the objects' property?  As it stands now, you can access it directly:

echo $example1->msg;

 

Pretty cool, right?  And the same goes for overwriting that value:

$example1->msg = "I have a lovely bunch of coconuts";

 

But therein lies the problem.  Any piece of code can just change the property directly.  What if you didn't want the $msg property to be changed unless you explicitly gave permission to these changes?  That's where the private keyword comes in.

 

Let's change the class to:

class Example
{
   private $msg = "I'm private now!";

   public function __construct()
   {
      echo "I'm the same as before";
   }

   public function getMsg()
   {
      return $this->msg;
   }

   public function setMsg($someText)
   {
      $this->msg = $someText;
   }
}

 

$msg itself is now safe from being accidentally accessed by some rogue code.  It can still be accessed and changed through the getMsg and setMsg functions.  Indeed, those functions force all of the code that wants to access the $msg property to do it in those particular ways, ensuring that the value isn't overwritten by accident elsewhere.

 

The same goes for private methods/functions.  Let's say each object needs to access the database upon construction:

class Example
{
   private $msg;

   public function __construct($id)
   {
      $initData = $this->accessDB($id);
      $this->msg = $initData['msg'];
   }

   private function accessDB($id)
   {
      $query = "SELECT * FROM mytable WHERE id = '$id'";
      $result = mysql_query($query);
      $data = mysql_fetch_assoc($result);

      return $data;
   }
}

// ....

$example = new Example(23);

 

The accessDB method is declared as private because it shouldn't be invoked by any other code in the script.  Instead, it should only be invoked when the object is constructed.

Link to comment
https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660104
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.