<?php
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo "Calling Public Within: <font color='blue'>$this->public </font><br>";
echo "Calling Protected within: <font color='blue'> $this->protected
</font><br>" ;
echo "Calling Private within:<font color='blue'> $this->private </font><br>
";
}
}
$obj = new MyClass();
echo "Object Calling Public Variable :<b>";
echo "<font color='red'> $obj->public </font></b><br>";
echo "<hr>";
//echo $obj->protected; // Fatal Error
//echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
echo "<hr>";
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
protected $protected = 'New Values';
function printHello()
{
echo " We can redeclare the public and protected method, but not private <br>";
echo "<font color='orange'>";
echo $this->public;
echo " ";
echo $this->protected;
echo "<br>Private won't show up:<u> $this->private ....</u>";
echo "</font>";
}
}
echo "extended class <br>";
$obj2 = new MyClass2();
echo "<font color='blue'><b>";
echo "Calling Public from Extended :$obj->public; <br>";// Works
echo "Calling Private from Extended :<font color='blue'>$obj2->private
</font><br>"; // Undefined
echo "</b></font>";
echo "<font color='green'><b>";
echo 'calling echo $obj2->protected will throw fatal error';
echo "</b></font>";
//echo $obj2->protected; // Fatal Error
echo " <hr>The object is calling a function in the extended class <br>";
$obj2->printHello(); // Shows Public, Protected2, not Private
?><?php
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo "Calling Public Within: <font color='blue'>$this->public </font><br>";
echo "Calling Protected within: <font color='blue'> $this->protected
</font><br>" ;
echo "Calling Private within:<font color='blue'> $this->private </font><br>
";
}
}
$obj = new MyClass();
echo "Object Calling Public Variable :<b>";
echo "<font color='red'> $obj->public </font></b><br>";
echo "<hr>";
//echo $obj->protected; // Fatal Error
//echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
echo "<hr>";
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
protected $protected = 'New Values';
function printHello()
{
echo " We can redeclare the public and protected method, but not private <br>";
echo "<font color='orange'>";
echo $this->public;
echo " ";
echo $this->protected;
echo "<br>Private won't show up:<u> $this->private ....</u>";
echo "</font>";
}
}
echo "extended class <br>";
$obj2 = new MyClass2();
echo "<font color='blue'><b>";
echo "Calling Public from Extended :$obj->public; <br>";// Works
echo "Calling Private from Extended :<font color='blue'>$obj2->private
</font><br>"; // Undefined
echo "</b></font>";
echo "<font color='green'><b>";
echo 'calling echo $obj2->protected will throw fatal error';
echo "</b></font>";
//echo $obj2->protected; // Fatal Error
echo " <hr>The object is calling a function in the extended class <br>";
$obj2->printHello(); // Shows Public, Protected2, not Private
?>