আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-5: Visibility or Access Modifier in PHP

What is Visibility or Access modifier in PHP?

Visibility or Access Modifier

Accessibility of any property, constant or method is determined by visibility. Visibility is usually declared before constant, property and method.

How many visibility or access modifier in PHP?

There are three types of visibility in PHP:

  • Public
  • Protected
  • Private

Now let’s try to understand each Visibility with example:

Public:

If we want to use any property, method and constant of any class within the class, in child class and outside of the class, then these constant, property and method have to be declared as public. Let’s get a better understanding by this example:

<?php
class GrandFather
{
    public $name='Masud Alam';  // A public variable
    public function getName(){
      return $this->name;
    }
}
 
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The public variable will be available to the inherited class
    }
 
}
 
$grandFa=new GrandFather;
 
echo $grandFa->getName(),"\n";
 
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam'
 
// Public variables can also be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // Prints 'Masud Alam
 
?>

Explanation: Observe, as our GrandFather class’s $name property is public, we were able to access the $name property from the current class, child class and even outside of the class.

Protected:

If we want to use any property, method and constant of any class within the class and it’s child class. then these constant, property and method have to be declared as protected. Let’s get a better understanding by this example:

<?php
class GrandFather
{
    protected $name='Masud Alam';  // A Protected variable
    public function getName(){
      return $this->name;
    }
}
 
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The Protected Property will be available to the inherited class
    }
 
}
 
$grandFa=new GrandFather;
 
echo $grandFa->getName(),"\n"; //Prints 'Masud Alam'
 
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam'
 
// Protected Property can not be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error
 
?>

Explanation: Observe, as our GrandFather class’s $name property is protected, we were able to access the $name property within the current class and it’s child class. But we were not able to access the $name property outside of the class or outside of it’s child class. If we do we will see a fatal error.

Private :

If we want to use any property, method and constant of any class only within the class itself and not in it’s child class or anywhere else. then these constant, property and method have to be declared as private. Let’s get a better understanding by this example:

<?php
class GrandFather
{
    private $name='Masud Alam';  // A Protected variable
    public function getName(){
      return $this->name;
    }
}
 
class Daddy extends GrandFather // Inherited class
{
    function displayGrandFaName()
    {
        return $this->name; // The Private Property will not available to the inherited class and will show notice
    }
 
}
 
$grandFa=new GrandFather;
 
echo $grandFa->getName(),"\n"; //Prints 'Masud Alam'
echo $gradFa->name, "\n"; //Results in a Notice 
 
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandFaName(),"\n"; // Results in a Notice 
 
// Protected Property can not be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandFather;
echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error
 
?>

Explanation: Observe, as our GrandFather class’s $name property is private, we were able to access the $name property only within the current class. But we were not able to access the $name property anywhere else. If we try to access it anywhere else we will see an error.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *