The main aim of Object Oriented Programming is to hide the data and provide security to the application users. Modifiers of classes are used to achieved them as they restrict the hackers without providing the access to the class members and data present in it. So, modifiers also called as "
Access Specifiers". Three access specifiers are available. They are:
Access Specifiers :
- Public
- Private
- Protected
The member of a class with a Private Access Specifier is not accessible and viewed outside of the class. The variables declared with a private keyword can be retrieved and accessed only by class and neighbor functions. By default, all the members of the class are declared as private.
The member of a class with Public Access Specifier is accessed
anywhere outside the class but only inside the program. The variables declared with a public keyword can be retrieved and accessed without member function.
The member of a class with Protected Access Specifier is not accessible and viewed outside the class same as Private Access Specifier. But, the advancement in protected is, the child class members can access it otherwise called as Derived Classes. It is a subclass of a class.
[php]
<?php
class person {
var $name;
public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name) {
$this->name = $persons_name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
[/php]