PHP Access Modifiers Example

  • public - the property or method can be accessed from everywhere. This is default
  • protected - the property or method can be accessed within the class and by classes derived from that class
  • private - the property or method can ONLY be accessed within the class
<?php  
class parent
{
public $name="websolutionstuff";
function_display()
{
echo $this->name."<br/>";
}
}
class child extends parent
{
function show()
{
echo $this->name;
}
}
$obj= new child;
echo $obj->name."<br/>";
$obj->function_display();
$obj->show();
?>
websolutionstuff
websolutionstuff
websolutionstuff
<?php  
class Websolutionstuff
{
private $name="websolutionstuff";
private function show()
{
echo "This is private method of parent class";
}
}
class child extends Websolutionstuff
{
function show1()
{
echo $this->name;
}
}
$obj= new child;
$obj->show();
$obj->show1();
?>
Fatal error:  Call to private method Websolutionstuff::show()....
<?php  
class Websolutionstuff
{
protected $a=200;
protected $b=100;
function add()
{
echo $sum=$this->a+$this->b."<br/>";
}
}
class child extends Websolutionstuff
{
function sub()
{
echo $sub=$this->a-$this->b."<br/>";
}
}
$obj= new child;
$obj->add();
$obj->sub();
?>
300
100

--

--

--

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Send E-mail Using Queue in Laravel 7/8

How to Send E-mail Using Queue in Laravel 7/8

How to restart a python program from inside

How to Restore The Original Layout of a Text Document after a Manipulation in Python

Complacency — The Developer’s Kiss of Death

Uploading your first project in GitHub (with images)!

Scrum is a powerful process for high school

Level Up Your Python Programming With Itertools

Installing Vmware Tools On Oracle Solaris 11

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

More from Medium

Laravel where date between

筆記 - Laravel Request Lifecycle

Laravel 8 Datatables Keeping Selected Page Number

Automatically delete read database notifications in Laravel