public, protected, and private Properties in PHP

By: Andi, Stig and Derick Viewed: 153436 times  Printer Friendly Format    


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.



Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. This is pretty straightforward and got me to under
View Tutorial          By: Renoir Boulanger at 2009-09-01 09:49:24

2. This is so phat. Maybe the best explanation ever t
View Tutorial          By: Devin Prince III at 2010-08-11 10:01:04

3. it's really amazing & very easy to understand
View Tutorial          By: Vaibhav Bhalerao at 2010-08-17 10:54:52

4. i disagree that private variables can be accessed
View Tutorial          By: Neeraj at 2010-10-22 07:23:56

5. Hey, this is a great explanation regarding the dif
View Tutorial          By: foam roofing at 2011-04-02 17:26:52

6. Rattling explanation .....thnx brother
View Tutorial          By: zorro at 2011-06-16 04:12:30

7. Rattling explanation .....thnx brother
View Tutorial          By: zorro at 2011-06-16 05:05:43

8. really nice
View Tutorial          By: umer at 2011-06-25 06:35:19

9. Been looking at several website for a clear explan
View Tutorial          By: Jase at 2011-08-09 12:00:10

10. Its super explanation
View Tutorial          By: jose at 2011-12-08 13:36:29

11. This is an informative tutorial.
Thanks for

View Tutorial          By: Amol Bhavsar at 2011-12-30 10:48:25

12. very nice session
View Tutorial          By: Rajendra singh rathod at 2012-03-16 13:31:07

13. very nice session
View Tutorial          By: Rajendra singh rathod at 2012-03-16 13:32:01

14. sdfsdfsdfsd
View Tutorial          By: jai at 2013-11-08 12:54:00

15. oqiogajo
View Tutorial          By: oqiogajo at 2017-01-17 18:51:01

16. OlegExhat
View Tutorial          By: OlegExhat at 2017-02-28 14:08:07

17. Clintoncow
View Tutorial          By: Clintoncow at 2017-04-07 02:56:43

18. Clintoncow
View Tutorial          By: Clintoncow at 2017-04-07 02:56:47

19. KostyaExhat
View Tutorial          By: KostyaExhat at 2017-04-09 13:02:24

20. Guestchoob
View Tutorial          By: Guestchoob at 2017-04-12 23:48:35

21. Kozelhdix
View Tutorial          By: Kozelhdix at 2017-05-03 08:55:37

22. KostyaExhat
View Tutorial          By: KostyaExhat at 2017-06-27 03:46:40