Translated by Google Translate

Public, Private, and Protected (Member Visibility) in OOP PHP



In OOP we will always find keywords Public, Private, and Protected. These three things are used to determine the visibility of members (properties and functions) that are characteristic of object-oriented programming. Are members globally accessible (public) or accessed by its class (private) or accessed by the class and subclasses. Meanwhile, if the procedural programming members can only be accessed globally.

A. Public

In public visibility, a method or property can be accessed globally. That means it can be accessed on the inside and outside of class or in other words the properties and functions are known globally. In OOP PHP tutorial for Beginers previously, we have made $objPeople which the visibility of properties and function are public.

This is another example of class with public visibility :

Script 1.
<?php
class student {
    public $sid;
    public $name;
    public $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

And this is the result looks like in browser
Picture 1. Public member visibility

B. Private

If public can accessed by global  then private visiblity means that the member (method or property) in a class are accessible only within its own class.

Let's modify the script at class student by changing the property visibility to be private.

Script 2
<?php
class student {
    private $sid;
   
private $name;
   
private $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

If we run the script 2, it will display an error message because the property sid, name and points have private visibility, so that the data accessed by outside of class can not be done. So how do the private property that is accessible outside of class so that it can be used by objects that need it? The solution is to use the function setter and getter.

Setter is used to create a (set of) value for a property. This function is generally used for properties that have private and protected visibility. Setter is often used to validate incoming data before the data is loaded on a property. While the getter is used to generate (get) a value of the member public, private, or protected from the result.

How to access private member with that function?

Script 3
<?php
class student {
    private $sid;
    private $name;
    private $point;

    function setSid($x) {
        $this->sid=$x;
    }
    function setName($x) {
        $this->name=$x;
    }
    function setPoint($x) {
        $this->point=$x;
    }
    function getSid() {
        return $this->sid;
    }
    function getName() {
        return $this->name;
    }
    function getPoint() {
        return $this->point;
    }

    function Point () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->setSid('3310901051');
$objStudent->setName('Hermanto');
$objStudent->setPoint(90);
$objStudent->point();
?>

In the script above, we use function setters and getters for each property that has a private visibility. Making the value of the function by taking the value setter and getter done by function. In the getter is used "return" value because the getter will return the value of the property taken by the data.

Note how to calling the property after instancing $objStudent, calling the property is done by calling the names function with ($objStudent-> setSid()). Although become longer code lines, but more OOP recommend this method for accessing a Member.

If the script is run in the browser then the result will be the same as the picture below :
Picture 2. Calling private visibility with setter & getter function

C. Protected

Protected almost SAME to the private that can only be accessed by its class and protected can also be accessed by its subclasses. Let's try changing the script 1 and we will change the visibility of the member be protected like the script below :

Script 4
<?php
class student {
    protected $sid;
   
protected $name;
   
protected $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

If the script is run in browser then the result will error : cannot access protected property.

The error occurred because the protected class can only be accessed by its class and subclasses. For more detailed discussion of inheritance protected in subclasses will be explained on the nexy tutorials on The Inheritance.




 

©2013 @namakuherman | Developed by Herman Creative Industry