What is Access Modifier?

The visibility of class members, (properties, methods), relates to how that member may be manipulated within, or from outside the class. Three levels of visibilty exist for class members.

  • public
  • private
  • Protected

  • Public Access :-

    By default, all class members are public. This means if you do not declare a property or method within the class, it will be public. It is good practice to declare the visibility of class members for the sake of readability for yourself and others. It is much easier for another programmer to see your intentions. This will also future proof your scripts should access modifiers be deprecated
    You can explicitly declare properties and methods to be public with the public keyword.

    <?php
    class Person
    {
    public $name;
    public function set_name($data)
    {
    $this->name=$data;
    }
    public function get_name()
    {
    return $this->name;
    }
    }
    $obj = new Person();
    $obj->set_name("mahtab");
    echo "Your Name is ", $obj->name, ".";
    ?>

    If a member variable and member function is declared public then it can access within the class and outside the class.
    Its scope is global.

    File name : index.php

    <?php
    class Test
    {
    public $mobileno;
    public function getno()
    {
    echo "$this->mobileno";
    }
    }
    $obj = new Test();
    $obj->mobileno = '7838897299';
    $obj->getno();
    ?>
    Output :- 7838897299

    Example :-

    File name : index.php

    <?php
    class Calculation
    {
    public $num;
    public function add()
    {
    return $this->num + 3;
    }
    }
    $obj = new Calculation();
    $obj->num=5;
    echo $obj->add();
    ?>
    Output : 8


    Private

  • You can make a class or object member private with the private keyword. When you make a member private, you can’t access it outside the class of object.
  • The keyword private protects code outside the object from modifying this value.
  • Private member variables are not accessible from outside the class. Because it can’t access these variable directly,
  • Private members are accessible only to the class itself.
  • Example :-

    File name : index.php

    <?php
    class Person {
    private $name;
    function set_name($data) {
    $this->name=$data; }
    function get_name() {
    return $this->name; }
    }
    $obj = new Person();
    $obj->set_name("mahtab");
    echo "Your Name is ", $obj->name, ".";
    ?>

    You get an error because the property you’re trying to access is private.

    Example :-

    Now everything works okay. You can also make methods private.
    <?php
    class Person
    { private $name;
    function set_name($data) {
    $this->name=$data; }
    private function get_name() {
    return $this->name;
    }
    }
    $obj = new Person();
    $obj->set_name("mahtab");
    echo "Your Name is ", $obj->get_name(), ".";
    ?>
    But now this code isn’t going to work, because you are trying to call get_name outside the object. And get_name method is private. This means that you can call it only from code inside the same object.

    Example :-

    <?php
    class Person
    { var $name;
    function set_name($data) {
    $this->name=get_name(); }
    private function get_name() {
    return $this->name; }
    }
    $obj = new Person();
    $obj->set_name("mahtab");
    echo "Your Name is ", $obj->get_name(), ".";
    ?>
    Fatal error, call to undefined function get_name().

    Example :-

    File name : index.php

    <?php
    class Calculation
    {
    private $num;

    public function add()
    {
    return $this->num + 3;

    }
    }


    $obj = new Calculation();

    $obj->num=5;

    echo $obj->add();
    ?>
    Output :- error. Because num is a private member.

    Example :-

    File name : index.php

    <?php
    class Demo
    {
    private $name;
    public function sayhello()
    {
    print "Hello " . $this->getName();
    }
    public function getName()
    {
    return $this->name;
    }
    public function setName($naam)
    {
    if(!is_string($naam) || strlen($naam) == 0 )
    {
    throw new Exception("Invalid Name value.");
    }
    $this->name=$naam;
    }
    }
    ?>

    File name : index.php

    <?php

    require_once 'Simple_Class.php';
    $obj = new Demo();
    $obj->setName("mahtab Alam");
    $obj->sayhello();
    $obj->setName(6567); // exception occurs
    ?>

    Output :-
    Hello mahtab alam

    Protected

  • Protected members are available to the class itself and to classes that inherit form it.
  • 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:
  • File name : index.php

    class Customer {protected $name;
    public function setName($name) { $this->name = $name;}
    public function getName() {return $this->name;}}
    class DiscountCustomer extends Customer{
    private $discount;
    public function setData($name, $discount) { $this->name = $name; //this is storing $name to the Customer //class $name variable. This works // as it is a protected variable
    $this->discount = $discount;}}

    File name : index.php

    $dc = new DiscountCustomer(); $dc->setData(“Stuart Broad”,10); echo $dc->name; // this does not work as $name is protected and hence // only available in Customer and DiscountCustomer class
    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.

    Using the protected keyword makes class members accessible only in the class they’re declared in, and any class derived from that class.

    Example :-

    File name : index.php

    For example
    <?php
    class Person
    {
    var $name;
    protected function set_name($data)
    {
    $this->nmae = $data;
    }
    function get_name( )
    {
    return $this->name;

    }
    }
    ?>
    That means that only code inside Person and classes derived from it can call the set_name().

    Example :-

    File name : index.php

    To make set_name accessible to code outside the object, you might create a new method, set_name_public. Which calls set_name internally.
    <?php
    class Person {
    var $name;
    protected function set_name($data) {
    $this->name=$data; }
    function get_name() {
    return $this->name; }
    }
    class Friend extends Person{
    var $mesage;
    function set_message($msg) {
    $this->message=$msg; }
    function speak() {
    echo $this->message;
    }
    function set_name_public($name)
    { $this->set_name($name);
    } }
    $obj = new Person("mahtab");
    $obj2 = new Friend;
    $obj2->set_name_public("mahtab");
    $obj2->set_message("Hello India");
    echo "My Name is ", $obj2->get_name(), ".<br>";
    echo $obj2->get_name()," says \"", $obj2->speak(), "\".<br>";
    ?>
    Now this works, because set_name_public has public access. Any code can call it.

    File name : index.php

    Note :-
    Public access gives all code access to a member, private restricts access to the current class, and protected restricted access to the current class and any classes derived from that class.

    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.





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here