Classes and Objects in PHP OOPS

<?php
class TV
{
    public $model = "xyz";
    public $volume = 1;
   
    //By default functions are public unless you define private or protected.
    public function volumeUp()
    {
        $this->volume++;
    }

    public function volumeDown()
    {
        $this->volume--;
    }
}

$tv_one = new TV();
$tv_two = new TV();

echo $tv_one->volume . "<br>";
echo $tv_two->volume . "<br>";
echo $tv_one->model . "<br>";
echo $tv_two->model . "<br>";
$tv_one->volumeUp();
echo $tv_one->volume . "<br>";
$tv_one->volumeDown();
echo $tv_one->volume . "<br>";

// OUTPUT :
//1
// 1
// xyz
// xyz
// 2
// 1
?>

Comments

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database