Access Specifiers in PHP

Access Specifiers in PHP play a very important role in OO(object-oriented) approach to programming. It is a key paradigm in OOP is encapsulation and access protection of object properties (also referred to as member variables). Most common OO languages have three main access restriction keywords: public, protected, and private.

When defining a class member in the class definition, the developer needs to specify one of these three access modifiers before declaring the member itself.

In case you are familiar with PHP 3 or 4’s object model, all class members were defined with the var keyword, which is equivalent to the public in PHP 5.

For example look at the following code for better understanding:

class MyClass {
 public $publicMember = "Public member";
 protected $protectedMember = "Protected member";
 private $privateMember = "Private member";
 function myMethod(){
 // ...
 }
}
$obj = new MyClass();

This example will be built upon to demonstrate the use of these access modifiers.


First, the more boring definitions of each access modifier:

  • public: Public members can be accessed both from outside an object by using $obj->publicMember and by accessing it from inside the myMethod method via the special $this variable. If another class inherits a public member, the same rules apply, and it can be accessed both from outside the derived class’s objects and from within its methods.
  • protected: Protected members can be accessed only from within an object’s method—for example, $this->protectedMember. If another class inherits a protected member, the same rules apply, and it can be accessed
    from within the derived object’s methods via the special $this variable.
  • private: Private members are similar to protected members because they can be accessed only from within an object’s method. However, they are also inaccessible from a derived object’s methods.

Usually, you would use public for members you want to be accessible from outside the object’s scope  and private for members who are internal to the object’s logic. Use protected for members who are internal to the object’s logic, but where it might make sense for inheriting classes to override them:

class MyDbConnectionClass {
 public $queryResult;
 protected $dbHostname = "localhost";
 private $connectionHandle;
 // ...
}
class MyFooDotComDbConnectionClass extends MyDbConnectionClass {
 protected $dbHostname = "foo.com";
}

This incomplete example shows a typical use of each of the three access modifiers.

Note: The access modifiers are designed so that classes (or more specifically, their interfaces to the outer world) always keep an is-a relationship during inheritance. Therefore, if a parent declares a member as public, the
inheriting child must also declare it as public.

Otherwise, the child would not have an is-a relationship with the parent, which means that anything you can do with the parent can also be done with the child.


public, protected, and private Methods

Access modifiers may also be used in conjunction with object methods, and the rules are the same:

  1. public methods can be called from any scope.
  2. protected methods can only be called from within one of its class methods or from within an inheriting class.
  3. private methods can only be called from within one of its class methods and not from an inheriting class. As with properties, private methods may be redeclared by inheriting classes. Each class will see its own version

of the method:

class MyDbConnectionClass {
 public function connect()
 {
 $conn = $this->createDbConnection();
 $this->setDbConnection($conn);
 return $conn;
 }
 protected function createDbConnection()
 {
 return mysql_connect("localhost");
 }
 private function setDbConnection($conn)
 {
 $this->dbConnection = $conn;
 }
 private $dbConnection;
}
class MyFooDotComDbConnectionClass extends MyDbConnectionClass {
 protected function createDbConnection()
 {
 return mysql_connect("foo.com");
 }
}

This skeleton code example could be used for a database connection class. The connect() method is meant to be called by outside code.

The createDbConnection() method is an internal method but enables you to inherit from the
class and change it; thus, it is marked as protected.

The setDbConnection() method is completely internal to the class and is therefore marked as private.

When no access modifier is given for a method, the public is used as the default. In the remaining chapters, the public will often not be specified for this reason

Leave a Comment

Your email address will not be published. Required fields are marked *