Public Private and Protected Using PHP OOP
What is Public Keyword in PHP:
the public property, Variable or method can be accessed from everywhere that's called as Public
Example:
<?php
/* Public Keyword*/
class pri
{
public $name='Public Work'; // A public variable
}
class pritwo extends pri // Inherited class
{
//private Not worked
function display()
{
return $this->name;
}
}
$s = new pritwo;
echo "<font color=red>".$s->display()."</font>";
$o = new pri;
echo "<font color=green>".$o->name."<font>";
?>
___________________________________________________________________
Private Keyword:
The private keyword that the declared property/method can only be accessed within the class
Private Keyword will be access in only limited scope
Example :
<?php
/* Private Keyword*/
class main
{
private $name='Private Work'; // A public variable
function display()
{
return $this->name;
}
}
class second extends main // Inherited class
{
function display()
{
return $this->name;
}
}
$s = new second;
echo "<font color=red>".$s->display()."</font>";
$o = new main;
echo "<font color=green>".$o->name."<font>";
?>
____________________________________________________________
Protected Keyword:
Protected Properties can be access within a inherited class
the property 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.
Example:
<?php
/* Protected Keyword*/
class m
{
protected $name='here Protected';
}
class s extends m // Inherited class
{
function display()
{
return $this->name;
}
}
$s = new s;
echo "<font color=red>".$s->display()."</font>";
$o = new m;
echo "<font color=green>".$o->name."<font>";
?>
___________________________________________________________________
learn all the Programming and Scripting Languages with youtube.com/avadhtutor
No comments:
Post a Comment