Private Methods in PHP

key

Key to Private Methods

Private Methods

The other day I was adding a private method to a PHP class, and I kept getting an unusual error. After multiple debugging attempts, I did a search on the Web and found the problem. (My problem for forgetting that PHP is different.) Like class properties the keyword $this needs to be involved.

Most readers of this blog probably know this already, but I’m including this little post to remind myself as much as anyone. Essentially, when you enter a private method in a class, it must be addressed as:

$this->pvtMethod();

In an of itself, that’s no great shakes, but if you don’t know that because you’ve immigrated to PHP from some other language such as C++, Java, ActionScript 3.0 or C#, you’re going to have a big problem with design patterns. That’s because in some design patterns you’re going to want to have private methods in some of the design’s participant classes.

Why Private Methods?

Why bother with a private method? Why bother with any private accessor or visibility? That’s easy:

Private visibility encapsulates your property or method.

As objects, we want everything in our classes to be treated like an integral part. If we want to give other classes access, we use getters and setters with public accessors. For example, consider the following program example:

<?php
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);
class PrivateProperties
{
	private $privateProp;
 
	function __construct()
	{
 
		print "From the constructor function <br />";
	}
 
	public function displayMethod()
	{
		print "This is from a public method.<br />";
 
		$this->privateProp=$this->privateMethod();
		print $this->privateProp;
	}
 
	private function privateMethod()
	{
		return "(Private Method): This only can be called from the class itself <br />";
	}	
}
$showtext=new PrivateProperties();
$showtext->displayMethod();

results:
From the constructor function
This is from a public method.
(Private Method): This only can be called from the class itself

The private method cannot be accessed directly from a call to it, but it can be launched from a public method within the same class. The function, displayMethod(), is public, but as part of the class, it can call the private methods in the same class. That’s exactly what it does.

While working with the private method, we don’t want to neglect a property with private visibility. In the same way that the $this statement is used with private methods, we also use it with private properties. As noted, this isn’t rocket science, but PHP is different from other languages, and this little reminder might come in handy when dealing with private visibility.

Share

Copyright © 2010 William Sanders. All Rights Reserved.

4 Responses to “Private Methods in PHP”


  • Do you have any insights on when to use private vs protected methods? The problem is private is extended classes don’t have access to them. It seems there may be some cases in which this is desirable, but that most time protected is a better choice. Do you have some guidelines?

  • Hi Yehosef,

    Great question. I like to use private variables/functions whenever possible to insure encapsulation. If I have an abstract class where I know I will be subclassing a lot, I’ll use protected methods/variables.

    You can use privately declared variables in protected methods or even public methods. For example,

    private $foo;
     
    public function bar()
    {
       $this->foo="hello";
       return $this->foo;
     
    }

    Kindest regards,
    Bill

  • how to call private method when it really needed just tell me one example .

  • Hi Venkatesh,

    Here is an example:

    class Vman
    {
       public function callPrivate( )
      {
         echo $this->privateMethod();
      }
     
      private function privateMethod()
      {
          return "It's Venkatesh!";
      }
    }

    When you run the example, you’d enter:

    $vman = new Vman();
    $vman->callPrivate();

    callPrivate Is a Public function that calls a Private function (method).

    If that does not help, let me know, and I’ll do another one for you.

    Kindest regards,
    Bill

Leave a Reply