In the PHP each and every property of a class in must have one of three visibility levels, known as public, private, and protected.
- Public: Public properties can be accessed by any code, whether that code is inside or outside the class. If a property is declared public, its value can be read or changed from anywhere in your script.
- Private: Private properties of a class can be accessed only by code inside the class. So if we create a property that’s declared private, only methods and objects inside the same class can access its contents.
- Protected: Protected class properties are a bit like private properties in that they can’t be accessed by code outside the class, but there’s one little difference in any class that inherits from the class i.e. base class can also access the properties.
Generally speaking, it’s a good idea to avoid creating public properties wherever possible. Instead, it’s safer to create private properties, then to create methods that allow code outside the class to access those properties. This means that we can control exactly how our class’s properties are accessed.
Note: If we attempt to access the property outside the class, PHP generates a fatal error.
PHP Access Specifier’s feasibility with Class, Sub Class and Outside World :
Class Member Access Specifier |
Access from own class |
Accessible from derived class |
Accessible by Object |
Private |
Yes |
No |
No |
Protected |
Yes |
Yes |
No |
Public |
Yes |
Yes |
Yes |
Below examples illustrate the Access Specifier of PHP:
- Example 1: In this example, we will see the Public Access Specifier.
php
<?php
class GeeksForGeeks
{
public $x = 100 ; # public attributes
public $y = 50 ;
function add()
{
echo $a = $this ->x + $this ->y ;
echo " " ;
}
}
class child extends GeeksForGeeks
{
function sub()
{
echo $s = $this ->x - $this ->y ;
}
}
$obj = new child;
$obj ->add() ;
$obj ->sub() ;
?>
|
150 50
- Example 2: In this example, we will see the Private Access Specifier
php
<?php
class GeeksForGeeks
{
private $a = 75 ; # private attributes
private $b = 5 ;
private function div() # private member function
{
echo $d = $this ->a / $this ->b ;
echo " " ;
}
}
class child extends GeeksForGeeks
{
function mul()
{
echo $m = $this ->a * $this ->b ;
}
}
$obj = new child;
$obj ->div();
$obj ->mul();
?>
|
PHP Fatal error: Uncaught Error: Call to private method
GeeksForGeeks::div() from context '' in /home/cg/root/8030907/
main.php:22 Stack trace:#0 {main} thrown in /home/cg/root/8030907/
main.php on line 22
- Example 3: In this example, we will see the Protected Access Specifier.
php
<?php
class GeeksForGeeks
{
protected $x = 1000 ; # protected attributes
protected $y = 100 ;
function div()
{
echo $d = $this ->x / $this ->y ;
echo " " ;
}
}
class child extends GeeksForGeeks
{
function sub()
{
echo $s = $this ->x - $this ->y ;
}
}
class derived # Outside Class
{
function mul()
{
echo $m = $this ->x * $this ->y ;
}
}
$obj = new child;
$obj ->div();
$obj ->sub();
$obj ->mul();
?>
|
10
900
Fatal error: Uncaught Error: Call to undefined method
child::mul() in /home/cg/root/8030907/main.php:32
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Sep, 2021
Like Article
Save Article