Unset Method

 <?php

//When we try to unset any class's private property from outside the class at that time
//__unset() method will automatically Call.

class Student
{
    public $course = "PHP";
    private $first_name;
    private $last_name;

    public function setName($fname, $lname)
    {
        $this->first_name = $fname;
        $this->last_name = $lname;
    }
    public function __unset($property)
    {
        unset($this->$property);
    }
}
$test = new Student();
$test->setName("Yahoo","Baba");
echo "<pre>";
print_r($test);
echo "</pre>";
echo "<br>";
unset($test->first_name);

echo "<pre>";
print_r($test);
echo "</pre>";

unset($test->course);
echo "<pre>";
print_r($test);
echo "</pre>";
// echo $test->course;     //Gives Error of Undefined Property
// echo $test->first_name; //Gives Error of Undefined Property

?>

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