In the earlier tutorials we have witnessed keywords like public, private and protected. These are nothing but access specifiers. So, lets understand what access specifiers are.
Definition of Access Specifiers
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.
Why do we need Access specifiers
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.
Explanation of each access specifier
1. Private
2. Protected
3. Public
2. Protected
3. Public
1. Private
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:
In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.
2. Public
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:
In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.
3. Protected
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:
In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.
You will learn more about inheritance later in this tutorial series. You should revisit this tutorial and read more on the protected section again when you understand inheritance better.
Important Note of Access Specifier in PHP5
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.
Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below
Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)
- PHP5 Tutorial – Learn to create a PHP5 Class
- PHP5 Tutorial – Learn to Create a PHP5 Class Object
- PHP5 Tutorial – Defining Attributes of a PHP5 Class
- PHP5 Tutorial – Defining Methods of a PHP5 Class
- PHP5 Tutorial – Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
- PHP5 Tutorial – Magic Methods – __toString() method
- PHP5 Tutorial – Magic Methods – __get() and __set()
- PHP5 Tutorial – Magic Methods – __isset() and __unset()
- PHP5 Tutorial – Magic Methods – __call() method
- PHP5 Tutorial – Magic Methods – __autoload() method
- PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
- PHP5 Tutorial – Magic Methods – __clone() method
No comments:
Post a Comment