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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- 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
Send push notifications using Expo tokens in PHP
PHP convert string to lower case
A Basic Example using PHP in AWS (Amazon Web Services)
Different versions of PHP - History and evolution of PHP
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
Resume or Pause File Uploads in PHP
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Comments