{"id":812,"date":"2018-11-15T03:40:27","date_gmt":"2018-11-15T03:40:27","guid":{"rendered":"https:\/\/barkhane.com\/php\/2018\/11\/15\/destructor-in-php\/"},"modified":"2024-03-03T14:54:34","modified_gmt":"2024-03-03T14:54:34","slug":"destructor-in-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/destructor-in-php\/","title":{"rendered":"Destructor in PHP"},"content":{"rendered":"<p>Destructor in PHP : PHP destructor allows you to clean up resources before PHP releases the object from the memory. For example, you may create a file handle in the <a href=\"https:\/\/barkhane.com\/php\/constructor-functions-in-php\/\">constructor<\/a> and you close it in the destructor.<\/p>\n<p>To add a destructor to a class, you just simply add a special method called\u00a0<code>__destruct()<\/code>\u00a0as follows:<\/p>\n<div id=\"crayon-5bece972df762855598661\" class=\"crayon-syntax crayon-theme-github crayon-font-sourcecodepro crayon-os-pc print-yes notranslate\" data-settings=\" no-popup minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\">\n<pre><code>public function __destruct()\n{\n\/\/ clean up resources here\n}\n<\/code><\/pre>\n<p>There are some important notes regarding the destructor:<\/p>\n<\/div>\n<\/div>\n<ul>\n<li>Unlike a constructor, a destructor cannot accept any argument.<\/li>\n<li>Object\u2019s destructor is called before the object is deleted. It happens when there is no reference to the object or when the execution of the script is stopped by the\u00a0<code>exit()<\/code>\u00a0function.<\/li>\n<\/ul>\n<p>The following simple\u00a0<code>FileUtil<\/code>\u00a0class demonstrates how to use the destructor to close a file handle. (Check it out the\u00a0PHP File I\/O tutorial\u00a0to learn how to deal with files in PHP).<\/p>\n<div class=\"crayon-plain-wrap\">\n<pre><code> &lt;? php\nclass FileUtil{\nprivate $handle;\nprivate $filename;\n\/**\n* inits file util with filename and mode\n* @param string $filename\n* @param string $mode\n*\/\npublic function __construct($filename,$mode){\n$this-&gt;filename = $filename;\n$this-&gt;handle = fopen($filename, $mode);\n}\n\/**\n* close the file handle\n*\/\npublic function __destruct(){\nif($this-&gt;handle){\nfclose($this-&gt;handle);\n}\n}\n\/**\n* display file content\n*\/\npublic function display(){\necho fread($this-&gt;handle, filesize($this-&gt;filename));\n}\n}\n$fu = new FileUtil(\u2018.\/test.txt\u2019, \u2018r\u2019);\n$fu-&gt;display();  ?&gt; <\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Destructor in PHP : PHP destructor allows you to clean up resources before PHP releases the object from the memory. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}