How to define and use class destructor in PHP

1 Answer

0 votes
class Test 
{
    public $pr = "Class property";
  
    public function __construct()
    {
        echo 'constructor activated <br />';
    }
    
    public function __destruct()
    {
        echo '<br /> destructor activated <br />';
    }
  
    public function setProperty($val)
    {
        $this->pr = $val;
    }
  
    public function getProperty()
    {
        return $this->pr;
    }
}
 
$obj1 = new Test;
 
echo $obj1->getProperty();
 
$obj1->setProperty("New property value");
 
echo "<br />";
echo $obj1->getProperty();
  
/*
run:
 
constructor activated
Class property
New property value
destructor activated 
 
*/

 



answered Oct 24, 2015 by avibootz

Related questions

1 answer 245 views
1 answer 242 views
242 views asked Mar 24, 2018 by avibootz
1 answer 286 views
286 views asked May 30, 2016 by avibootz
1 answer 337 views
3 answers 457 views
1 answer 308 views
308 views asked Jun 28, 2015 by avibootz
...