{"id":84,"date":"2023-05-06T03:17:03","date_gmt":"2023-05-06T03:17:03","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=84"},"modified":"2023-05-06T03:17:03","modified_gmt":"2023-05-06T03:17:03","slug":"what-is-constructor-and-destructor-in-php","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/06\/what-is-constructor-and-destructor-in-php\/","title":{"rendered":"What is constructor and destructor in PHP?"},"content":{"rendered":"\n<p>In object oriented programming terminology, constructor is a method defined inside a class that is called automatically at the time of creation of the object. Purpose of a constructor method is to initialise the object. In PHP, a method of special name __construct acts as a constructor.<\/p>\n\n\n\n<p>__construct ([ mixed $args = &#8220;&#8221; [, $&#8230; ]] ) : void<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>class myclass{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;function __construct(){<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;object initialised&#8221;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>$obj=new myclass();<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>This will produce the following result. \u2212<\/p>\n\n\n\n<p>object initialised<\/p>\n\n\n\n<p><strong>Destructor<\/strong><\/p>\n\n\n\n<p>Destructor is a method automatically as soon as a garbage collector finds that a particular object has no more references. In PHP, the destructor method is named as __destruct. During the shutdown sequence too, objects will be destroyed. Destructor method doesn&#8217;t take any arguments, neither does it return any data type<\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>class myclass{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;function __construct(){<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;object is initialised<\/p>\n\n\n\n<p>&#8220;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;function __destruct(){<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;object is destroyed<\/p>\n\n\n\n<p>&#8220;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>$obj=new myclass();<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<p>This will show following result<\/p>\n\n\n\n<p>object is initialised<\/p>\n\n\n\n<p>object is destroyed<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In object oriented programming terminology, constructor is a method defined inside a class that is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/84"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/84\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}