Showing posts with label PHP Access modifiers. Show all posts
Showing posts with label PHP Access modifiers. Show all posts

Tuesday, February 18, 2014

PHP Access Modifiers - 3

class Person
{
 public function pub()
 {
  echo 'I am Publicly accessible<br>';
 }

 private function pri()
 {
  echo 'I am Private function<br>';
 }
 protected function pro()
 {
  echo 'I am Protected function<br>';
 }
}



class NewPerson extends Person
{

 public function getPrivatedata()
 {
  parent::pri();
 }

 public function getProtectedata()
 {
  parent::pro();
 }

}



$newperson = new NewPerson;



//$newperson->getPrivatedata();  // Fatal error: Call to private method Person::pri() from context 'NewPerson'



$newperson->getProtectedata();  // Output is I am Protected function




from the above example newperson object trying to access a function , that is defined as private from its class Person. that shows error message. because we cant access it from derived class.

after that we call a function getProtectedata() . it access protected function pro from Person. It can be accessible , because of protected.

so whenever we want to access a function from its child class, it should be defined as protected, else private.

PHP - Access modifiers - 2

class Person
{

 public function pub()
 {
  echo 'I am Publicly accessible<br>';
 }

 private function pri()
 {
  echo 'I am Private function<br>';
 }

 protected function pro()
 {
  echo 'I am Protected function<br>';
 }
}

$person = new Person;



$person->pub();  // output is I am Publicly accessible

$person->pri();  // Fatal error: Call to private method Person::pri()

$person->pro();  // Fatal error: Call to protected method Person::pro()

PHP - Access modifiers - 1


PHP - Access modifiers

 Access modifiers are used to access class members (variables and methods).

Acess modifiers are,

*  Public
*  Private
*  Protect

 Public variable and method can accessible from anywhere .

 Private members only accessible inside the class not the inherited class.

 In other terms private access control is used to maintain encapsulation functionality.

Protected members can only access from derived classes.


class Person
{
 public $name;
 protected $age;
 private $phone;
 
 function setName($name)
 {
  $this->name=$name;
 } 
 
 function getName()
 {
  return $this->name;
 }
 
 function setAge($age)
 {
  $this->age=$age;
 }
 

 function getAge()
 {
  return $this->age;
 }

 function setPhone($phone)
 {
  $this->phone=$phone;
 } 

 function getPhone()
 {
  return $this->phone;
 }


}

$person = new Person;



$person->setName('Sivanthi');
$person->setAge(28);
$person->setPhone('95******53');
echo $person->getName();  // output is  Sivanthi

echo $person->getAge();  // output is  28

echo $person->getPhone();  // output is  95******53

echo "Person Name is ".$person->name;  // output is Person Name is Sivanthi

echo "Person Age is ".$person->age; // Cannot access protected property Person::$age

echo "Person Phone is ".$person->phone; // Cannot access protected property Person::$phone


So we cannot directly access private and protected members from a class,

instead of we can use setters and getters to access those variables.