Broad Network


Visibility in PHP Classes

Object Oriented Programming in PHP – Part 6

Forward: In this part of the series I explain visibility in PHP classes.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 6 of my series, Object Oriented Programming in PHP. The keywords, public, protected, and private can be considered as access specifiers in PHP classes. In PHP, access specifier comes under the topic, visibility. In this part of the series I explain visibility in PHP classes. We have seen one of the access specifiers: public. In this part of the series we shall see the meanings of all three and their applications.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Class Structure
The structure of a class is as follows:

class className
    {
        accessspecifier property;
         ...
        accessspecifier method{}
         ...
    }

An access specifier is the keyword, public or protected or private. Every member (property or method) should be preceded by an access specifier.

If no access specifier is there (typed) for a method, then the default access specifier, assumed for the method is public.

What accesses the Class Members?
Members of a class can access other members (properties and methods) of the same class description. Functions, operators, statements and other classes (objects) outside the class description of a particular class can also access members of that class, when the class is an object. An access specifier decides whether or not a function or operator or class, inside or outside the class description can access the member it controls inside its class. The member an access specifier controls is the member typed next to it in the class description.

I will use statements, functions and classes in the illustrations of access to class members. I will not use operators for the illustrations.

I will be using the phrase, “external function”. This refers to a function or class method that is not a member of the class description in question. I will also use the phrase “external statement” for the same reason. When I say an external function can access a class member, I mean the external function can use the name (variable of property or name of method) of the member as its argument or as a variable inside its definition.

The public Access Specifier
With the public access specifier, an external function can access the public members of the class. The following code illustrates this (read the explanation below):

<?php

    class Calculator
        {
            public $num1;
            public $num2;
    
            public function add()
                {
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

    function myFn($par)
        {
            return $par;
        }


        $obj = new Calculator();
        $obj->num1 = 2;
        $obj->num2 = 3;
        $result = $obj->add();
        echo $result . "<br />";
        $myVar = myFn($obj->num1);
        echo $myVar;
    
?>

There is one function in the code: myFn(). You also have statements that are not in any function. The first line among the free statements instantiates the class object called, obj. Lines 2 and 3 of the free statements use the properties of the class like variables. Because the class members are public, the free statements can access the members of the class. We have seen things like this before, but now, I am using them to explain the specifier, public. Line 4 of the free statements also demonstrates this. In line 6 of the free statements, the function, myFn() uses the property num1 of the class as its argument. It could do so because the member, num1 is public in the class.

The private Access Specifier
With the private access specifier an external function or an external statement cannot access the private members of the class. With the private specifier only a member of a class description can access the private member of the class description. The following code shows how only a member of a class can access a private member of the class (read the explanation below):

<?php

    class Calculator
        {
            private $num1;
            private $num2;
    
            public function add()
                {
                    $this->num1 = 2;
                    $this->num2 = 3;
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

        $obj = new Calculator();
        $result = $obj->add();
        echo $result;
    
?>

The class has two private members (properties) and one public member (method). In the class description, the add() method definition uses the names of the private members like variables (e.g. $this->num1). So the add() method, a member of the class description has accessed the private members of the class description.

The second line of the free statements outside the class description has been able to access the add() method of the class because the add() method is public (it has a public access specifier).

The following code will not compile because the second free statement tries to access (use as variable) a private member of the class:

<?php

    class Calculator
        {
            private $num1;
            private $num2;
    
            public function add()
                {
                    $this->num1;
                    $this->num2 = 3;
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

        $obj = new Calculator();
        $obj->num1 = 2;
        $result = $obj->add();
        echo $result;
    
?>

The second line among the free statements is wrong because it tries to access (use as variable) the private member, num1.

The protected Access Specifier
If a member of a class is public, it can be accessed by an external function and external statement and also a derived class description. If a member of a class is private, it cannot be accessed by an external function or external statement; even a derived class cannot access it.

The question is, should a derived class not really be able to access a private member of its base class (since the derived class and base class are related)? Well, to solve this problem you have another access specifier called, protected. If a member of a class is protected, it can be accessed by a derived class, but it cannot be accessed by an external function or external statement. It can also be accessed by members within the class description (base class). The following code illustrates how a derived class can access a protected member of a base class:

<?php

    class Calculator
        {
            protected $num1;
            protected $num2;
        }
    
    class ChildCalculator extends Calculator
        {
            public function add()
                {
                    $this->num1 = 2;
                    $this->num2 = 3;
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

        $chObj = new ChildCalculator();
        $result = $chObj->add();
        echo $result;

?>

The base class has just two properties and no method; these properties are protected. The derived class has one method and no property. Inside the derived class, the protected properties of the base class are used as variables. Generally, when a derived class is using a member of a base class, it is a method of the derived class that is using the member, as in this example. The above code is OK.

The following code will not compile, because line 2 among the free statements tries to access a protected member of a class (base class):

<?php

    class Calculator
        {
            protected $num1;
            protected $num2;
        }
    
    class ChildCalculator extends Calculator
        {
            public function add()
                {
                    $this->num1;
                    $this->num2 = 3;
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

        $obj = new Calculator();
        $obj->num1 = 2;
        $chObj = new ChildCalculator();
        $result = $chObj->add();
        echo $result;

?>

An external function or external statement cannot access a protected member of a class (base class); however, a derived class can access a protected member of the base class.

Note: A member of a class description can access any member of the same class description, independent of whether the member is public, protected or private.

You should now know the role of the access specifiers: public, protected and private as applied to classes.

A public member of a class is accessible by external functions, external statements and a derived class. A private member of a class is accessible only by other members of the same class; it is not accessible by external functions and external statements and it is not accessible by a derived class. A protected member of a class is accessible by a derived class (and other members of the class); it is not accessible by external functions or external statements.

Let us stop here for this part of the series and continue in the next.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message