Sun May 24 2020

Constructor Destructor

PHP Scripting862 views

File Name: constructor-destructor.php

<?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

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.