Inheritance in PHP OOPS
<?php
class TV
{
public $model;
public $volume;
public function volumeUp()
{
$this->volume++;
}
public function volumeDown()
{
$this->volume--;
}
//Here Below constructor(of class TV) will be override because there is also a
// constructor(of class TvWithTimer) present inside TvWithTimer Class as shown below :
public function __construct($m, $n)
{
$this->model = $m;
$this->volume = $n;
}
}
class TvWithTimer extends TV
{
public $timer = true;
public function __construct($a, $b)
{
echo "Hello $a, this is TvWithTimer class constructor and value is $b";
}
}
class plazmaTv extends TV
{
public $plazmaVar = true;
public $plazmaVarTwo = false;
public $model = "def";
}
$tv = new TvWithTimer("abc", 2);
$plazma = new plazmaTv("plazma Tv", 3);
//Here below the value of "model" variable will be override
//because First "model" value is "def" in plazmaTv class and
//second "model" value is "Hello" as shown below :
$plazma->model = "Hello";
echo $plazma->model . "<br>";
echo $tv->model . "<br>";
echo $tv->volume . "<br>";
echo $plazma->model . "<br>";
echo $plazma->volume . "<br>";
echo $plazma->plazmaVar . "<br>";
echo $plazma->plazmaVarTwo . "<br>";
// OUTPUT :
//Hello abc, this is TvWithTimer class constructor and value is 2Hello
// Hello
// 3
// 1
?>
Comments
Post a Comment