public, protected, and private Properties in PHP
By: Andi, Stig and Derick
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.
Archived Comments
1. KostyaExhat
View Tutorial By: KostyaExhat at 2017-06-27 03:46:40
2. Kozelhdix
View Tutorial By: Kozelhdix at 2017-05-03 08:55:37
3. Guestchoob
View Tutorial By: Guestchoob at 2017-04-12 23:48:35
4. KostyaExhat
View Tutorial By: KostyaExhat at 2017-04-09 13:02:24
5. Clintoncow
View Tutorial By: Clintoncow at 2017-04-07 02:56:47
6. Clintoncow
View Tutorial By: Clintoncow at 2017-04-07 02:56:43
7. OlegExhat
View Tutorial By: OlegExhat at 2017-02-28 14:08:07
8. oqiogajo
View Tutorial By: oqiogajo at 2017-01-17 18:51:01
9. sdfsdfsdfsd
View Tutorial By: jai at 2013-11-08 12:54:00
10. very nice session
View Tutorial By: Rajendra singh rathod at 2012-03-16 13:32:01
11. very nice session
View Tutorial By: Rajendra singh rathod at 2012-03-16 13:31:07
12. This is an informative tutorial.
Thanks for sharing your knowledge with us.
Th
View Tutorial By: Amol Bhavsar at 2011-12-30 10:48:25
13. Its super explanation
View Tutorial By: jose at 2011-12-08 13:36:29
14. Been looking at several website for a clear explanation of the difference. You make is so clear &
View Tutorial By: Jase at 2011-08-09 12:00:10
15. really nice
View Tutorial By: umer at 2011-06-25 06:35:19
16. Rattling explanation .....thnx brother
View Tutorial By: zorro at 2011-06-16 05:05:43
17. Rattling explanation .....thnx brother
View Tutorial By: zorro at 2011-06-16 04:12:30
18. Hey, this is a great explanation regarding the differences between public, private & protected m
View Tutorial By: foam roofing at 2011-04-02 17:26:52
19. i disagree that private variables can be accessed from derived class. The below code runs perfectly.
View Tutorial By: Neeraj at 2010-10-22 07:23:56
20. it's really amazing & very easy to understand & grasp the concept,thanks!!
View Tutorial By: Vaibhav Bhalerao at 2010-08-17 10:54:52
21. This is so phat. Maybe the best explanation ever to be invented and shared with the entire world wid
View Tutorial By: Devin Prince III at 2010-08-11 10:01:04
22. This is pretty straightforward and got me to understand this basic and outline in clearly in my mind
View Tutorial By: Renoir Boulanger at 2009-09-01 09:49:24
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
PHP convert string to lower case
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Error: Length parameter must be greater than 0
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8