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

Ajax Insert Records in Database Tutorial in PHP

Displaying Database data using Class, Object, Constructor, Destructor in PHP MySql

SubQuery with EXISTS and NOT EXISTS in PHP