Constructor Destructor

PHP Programming

PHP Programming Examples

<?php
	class area {
		private $ans = 0;

		/* Constructor of the class with parameter */
		function __construct($width, $height) {
			$this->ans = $width * $height;
		}
		
		/* Method of the class */
		function display() {
			return $this->ans;
		}
		
		/* Destructor of the class */		
		function __destruct() {
        echo '<br />Destroying object';
    	}
	}

	/* Create object of the class by calling constructor */
	$rect = new area(5,4);
	
	/* Calling member function */
	echo "Area of a Rectangle: ".$rect->display();
?>


          /****** Output ******/
          

Area of a Rectangle: 20


Destroying object

Learn more about constructor and destructor