PHP Visibility

Securing any web application is very important. If your web application is not secure, then nobody can easily break it and access your users’ private and important data.

For example classes are store as important data properties related to your application. Functions performed by functions on this data.

These properties and functions can be easily access by the object outside the class. In this way your data is not secure.

PHP provides you control access mechanism to secure data. This mechanism called visibility. By Visibility you can control access to all the properties and methods.

PHP Visibility Modes

The visibility mechanism of PHP works in 3 modes. These modes define different access levels. Programmers can apply any mode of access level to properties and methods according to need.

Visibility modes are available as keywords that you define while declaring properties and methods.

Syntax

The general syntax for applying visibility modes on properties being give below.

[visibilityMode] [propertyName] = [constantValue];

The general syntax for applying visibility modes to the functions is being give below.

[visibilityMode] [functionKeyword] [functionName] (arguments-list)
{
// Function body
}

All the visibility modes being explain in detail.

Public

When you declare a property or method with a public visibility mode, the conditions given below applied to them.

  • They can be accessed within the class in which they are declare.
  • In the class they are declare, they can be access within any class that inherits.
  • They can be access outside of the class they are declare.

Example

<?php
// Parent class
class myClass
{
public $name = ‘Jassi Gill’;
}

// Child Class
class subClass extends myClass
{

}

// Parent class object
$obj = new myClass();

// Sub class object
$subObj = new subClass();

// Accessing public property outside class. Valid.
echo $obj->name.”<br />”;

// Accessing public property of parent class. Valid.
echo $subObj->name; 

?>

As you can see in the above example, public declare properties can be easily accessed outside the class.

You should declare those properties and functions publicly which are not important or that you need publicly access to interact with your web application. This example produces the below given output.

Output

Jassi Gill
Jassi Gill

Protected

When you declare a property or method with a protected visibility mode, the conditions given below apply to them.

  • They can be access within the class they are declare.
  • The class in which they are declare can be access within any class that they inherit.

Example

<?php
// Parent class
class myClass
{
protected $name = ‘Shri Narendra Modi’;
}

// Child class
class myOtherClass extends myClass
{


 
public function displayName()
{
// Accessing protected member of parent class in subclass. Valid.
return $this->name;
}

}

// Parent class object
$obj = new myClass;

// echo $obj->name;   Invalid will generate an error.
// Child class object

$otherObj = new myOtherClass;

// Accessing public function outside class. Valid.
echo $otherObj->displayName(); 

?>

As you can see in the example above, protecte properties can not be accessed outside the class.

If you want to be accessed by a property or function sub classes, you should declare them securely. This example produces the below given output.

Output

Shri Narendra Modi

Private

When you declare a property or method with a private visibility mode, the conditions given below apply to them.

  • The class in which they are declare, can be access only within that class.

Example

<?php
class myClass
{

// Private property
private $name=’Shri Narendra Modi’;

public function display()
{
// Accessing private property inside class. Valid.
echo “$this->name”;
}

}

class myOtherClass extends myClass
{

public function displayName()
{
// return $this->name;  //Invalid, subclass cannot access private properties
}

}

$obj = new myClass;
//echo $obj->$name; // Not Valid : Property is private can not access outside class
echo $obj->display(); // Valid accessing through public function

?>

As you see in the above example, private properties can not be accessed outside the class. They can only be access within the same class in which they are declare.

When your data is important, then you should declare that property private. By doing so, only that property can be accessed within the class. The above example produces the below given output.

Output

Shri Narendra Modi