Object Visibility



The concept of visibility is a concept in which object accessibility (attributes, methods) can not be accessed in Object Oriented Programming. We could access the quality methods in our sample class from the previous location out of the classroom. Why do we need to change the visibility of objects? When class designs are performed, some methods need to be used only in the classroom. Because these methods contain code related to the structure of the class. We do not want to interfere with the way in which such code group takes place. Therefore, the visibility of classroom qualities and methods may need to be changed according to class design.

There are 3 view specifiers used in general programming languages ;

the public

Methods and attributes with this lookahead can be accessed from outside the classroom.

protected

Methods and attributes with this lookahead can be accessed from the top and bottom classes.

private

Methods and attributes with the view expression can only be accessed within the same class.

 

 

# Public


objects that have this view can be accessed from outside the classroom. The most comprehensive overview is the expression.

File: Car.php
class Car
{
    public $name;
    
    public function setName(string $name)
    {
        $this->name = $name;    
    }

    public function getName() : string
    {
        return $this->name;
    }
}

Suppose we have an example class as above. Since all attributes and methods of this class are specified with public visibility, we will be able to control them after class usage.

File: index.php
<?php require 'Car.php';

$car = new Car;

$car->setName('Bugatti');

echo $car->getName();
Bugatti

 It would be more appropriate to set the $ name property public in the Car class to private, at least protected . Because a setName () method that can change this value is not the correct application to be public as it already exists . This can lead to undesirable uses that may corrupt the operation of the class.

File: index.php
<?php require 'Car.php';

$car = new Car;

$car->setName('Bugatti');

$car->name = 'Ferrari';

echo $car->getName();
Ferrari

 

 

# Protected


this makes objects with accessibility accessible from the top and bottom classes, including the class itself. It is not possible to use it outside the classroom. Let's continue with our car class example above . For example the C class public expressed in $ name attribute SetName () because it can be manipulated by the method public is unnecessary to be expressed in.

File: Car.php
class Car
{
    protected $name;
    
    public function setName(string $name)
    {
        $this->name = $name;    
    }

    public function getName() : string
    {
        return $this->name;
    }
}

In the example above, $ name will no longer be accessible from outside.

File: index.php
<?php require 'Car.php';

$car = new Car;

$car->setName('Bugatti');

echo $car->getName();
Bugatti

If the view tries to be accessed from outside of a public attribute that is not set or a method access area, the system returns a fatal fatal error.

<?php require 'Car.php';

$car = new Car;

$car->setName('Bugatti');

$car->name = 'Ferrari';

echo $car->getName();
Fatal error : Uncaught Error: Can not access protected property Car :: $ name in C: \ xampp \ htdocs \ test.php: 23 Stack trace: # 0 {main} thrown in  C: \ xampp \ htdocs \ test.php  on line 4

Protected objects can not be accessed from outside the class, but can still be accessed from the parent class. We will refer to the concept of upper class later.

 

 

# Private


The lowest comprehensive visibility statement. The attributes and methods that have this view are never accessible outside the same class. Take care to use this view in this situation.

class Car
{
    private $name;
}

In the above example, $ name is no longer accessible from the outside or top level.

<?php require 'Car.php';

$car = new Car;

$car->name = 'Ferrari';
Fatal error : Uncaught Error: Can not access private property Car :: $ name in C: \ xampp \ htdocs \ test.php: 10 Stack trace: # 0 {main} thrown in  C: \ xampp \ htdocs \ test.php  on line  3