Programming Tutorials

public, protected, and private Properties in PHP

By: Andi, Stig and Derick in PHP Tutorials on 2008-11-22  

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 public in PHP 5. var has been kept for backward compatibility, but it is deprecated, thus, you are encouraged to convert your scripts to the new keywords:

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 (for example, $this->publicMember). 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. Because private properties aren't visible from inheriting classes, two related classes may declare the same private properties. Each class will see its own private copy, which are unrelated.

Usually, you would use public for members you want to be accessible from outside the object's scope (i.e., its methods), 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 typical use of each of the three access modifiers. This class manages a database connection including queries made to the database:

  • The connection handle to the database is held in a private member, because it is used by the class's internal logic and shouldn"t be accessible to the user of this class.

  • In this example, the database hostname isn't exposed to the user of the class MyDbConnectionClass. To override it, the developer may inherit from the initial class and change the value.

  • The query result itself should be accessible to the developer and has, therefore, been declared as public.

Note that access modifiers are designed so that classes (or more specifi-cally, 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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

Latest Articles (in PHP)