{"id":3891,"date":"2018-10-09T11:00:55","date_gmt":"2018-10-09T11:00:55","guid":{"rendered":"http:\/\/ebhor.com\/?p=3891"},"modified":"2019-11-08T11:28:58","modified_gmt":"2019-11-08T11:28:58","slug":"php-constructor-and-destructor","status":"publish","type":"post","link":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/","title":{"rendered":"PHP Constructor and Destructor"},"content":{"rendered":"\n

If the user wants to access any member function of a class then he or she must create the object<\/g> first then call the member function by this object.<\/p>\n\n\n\n

The constructor<\/g> is a method or way which automatically called when an object the for a class.<\/p>\n\n\n\n

No need to call it by an object. Simply constructor is a function which called when object<\/g> creates of class.<\/p>\n\n\n\n

Constructor declares by two ways.<\/p>\n\n\n\n

  1. Define function name same as class name. (This is old way to declare constructor. It is use in PHP 5.3.3 version)<\/li>
  2. Define function by __construct keyword.<\/li><\/ol>\n\n\n\n

    Only one constructor creates in one class. It is used to run an<\/g> operation which needs to run at first of any other operation.<\/p>\n\n\n\n

    Like Database Connection. The constructor<\/p>\n\n\n\n

    Example:<\/strong><\/p>\n\n\n\n

    \ncpu = $c;\n\t\t $this->ram = $r;\n\t\t $this->hdd = $h;\n\t }\n\t function describe()\n\t {\n       echo \"Hello, I am a Computer. My CPU is $this->cpu GHz, with $this->ram GB RAM and $this->hdd GB Hard Disk\";  \n\t }\n }\n $dell = new Computer(2.4, 4, 512);\n $dell->describe();\n?>\n<\/pre>\n\n\n\n

    Output:<\/strong><\/p>\n\n\n\n

    \nHello, I am a Computer. My CPU is 2.4GHz, with 4 GB RAM and 512 GB Hard Disk.\n<\/pre>\n\n\n\n

    Explanation:<\/g><\/strong><\/p>\n\n\n\n

    In above example $dell is an object of Computer class when its created constructor automatically called and CPU, RAM and Hard disk value passed by parameter.<\/p>\n\n\n\n

    Then when called describe() function its return the whole value<\/p>\n\n\n\n

    Destructor<\/strong><\/h2>\n\n\n\n

    the <\/g> is also a method or way in OOPS which is opposite of Constructor.<\/p>\n\n\n\n

    Destructor also called automatically when object an of any class but it\u2019s run when The constructor creates.<\/p>\n\n\n\n

    It the for free up memory. Using this after complete a task unset all variable and re The constructor<\/g><\/g> it in next<\/g> program.<\/p>\n\n\n\n

    Destructor defines by __destruct keyword. an <\/g> is also a function which automatically called when object The constructor.<\/p>\n\n\n\n

    Example:<\/strong><\/p>\n\n\n\n

    \nI'm dead now :(\";\n    }\n\n    public function __construct($name)\n    {\n        echo \"I'm alive!\";    \n        $this->name = $name;\n    }\n}\n$animal = new Animal(\"Bob\");\necho \"Name of the animal: \" . $animal->name;\n?>\n<\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    I'm alive! Name of the animal: Bob\nI\u2019m dead now \uf04c\n\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"

    If the user wants to access any member function of a class then he or she must create the object first then call the member function by this object. The constructor is a method or way which automatically called when an object the for a class. No need to call it by an object. Simply constructor is a … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[188,220,7],"yoast_head":"\nPHP Constructor and Destructor - Ebhor.com<\/title>\n<meta name=\"description\" content=\"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Constructor and Destructor - Ebhor.com\" \/>\n<meta property=\"og:description\" content=\"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\" \/>\n<meta property=\"og:site_name\" content=\"Ebhor.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ebhorcom\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-09T11:00:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-08T11:28:58+00:00\" \/>\n<meta name=\"author\" content=\"manish\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"manish\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\"},\"author\":{\"name\":\"manish\",\"@id\":\"https:\/\/www.ebhor.com\/#\/schema\/person\/6a63908fdbbb27289970569ebd2e709b\"},\"headline\":\"PHP Constructor and Destructor\",\"datePublished\":\"2018-10-09T11:00:55+00:00\",\"dateModified\":\"2019-11-08T11:28:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\"},\"wordCount\":247,\"publisher\":{\"@id\":\"https:\/\/www.ebhor.com\/#organization\"},\"keywords\":[\"constructor\",\"destructor\",\"php\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\",\"url\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\",\"name\":\"PHP Constructor and Destructor - Ebhor.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.ebhor.com\/#website\"},\"datePublished\":\"2018-10-09T11:00:55+00:00\",\"dateModified\":\"2019-11-08T11:28:58+00:00\",\"description\":\"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way\",\"breadcrumb\":{\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.ebhor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Constructor and Destructor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.ebhor.com\/#website\",\"url\":\"https:\/\/www.ebhor.com\/\",\"name\":\"Ebhor.com\",\"description\":\"Read The Latest Post Java, Css, Html, Php learning articles\",\"publisher\":{\"@id\":\"https:\/\/www.ebhor.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.ebhor.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.ebhor.com\/#organization\",\"name\":\"Ebhor.com\",\"url\":\"https:\/\/www.ebhor.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.ebhor.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.ebhor.com\/wp-content\/uploads\/2021\/05\/cropped-cropped-cropped-ebhor_logo_100x.jpg\",\"contentUrl\":\"https:\/\/www.ebhor.com\/wp-content\/uploads\/2021\/05\/cropped-cropped-cropped-ebhor_logo_100x.jpg\",\"width\":100,\"height\":100,\"caption\":\"Ebhor.com\"},\"image\":{\"@id\":\"https:\/\/www.ebhor.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ebhorcom\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.ebhor.com\/#\/schema\/person\/6a63908fdbbb27289970569ebd2e709b\",\"name\":\"manish\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.ebhor.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/def43b1d916cff76638fa6c47604a2bb?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/def43b1d916cff76638fa6c47604a2bb?s=96&r=g\",\"caption\":\"manish\"},\"description\":\"A Learner and Teacher\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Constructor and Destructor - Ebhor.com","description":"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/","og_locale":"en_US","og_type":"article","og_title":"PHP Constructor and Destructor - Ebhor.com","og_description":"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way","og_url":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/","og_site_name":"Ebhor.com","article_publisher":"https:\/\/www.facebook.com\/ebhorcom\/","article_published_time":"2018-10-09T11:00:55+00:00","article_modified_time":"2019-11-08T11:28:58+00:00","author":"manish","twitter_card":"summary_large_image","twitter_misc":{"Written by":"manish","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#article","isPartOf":{"@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/"},"author":{"name":"manish","@id":"https:\/\/www.ebhor.com\/#\/schema\/person\/6a63908fdbbb27289970569ebd2e709b"},"headline":"PHP Constructor and Destructor","datePublished":"2018-10-09T11:00:55+00:00","dateModified":"2019-11-08T11:28:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/"},"wordCount":247,"publisher":{"@id":"https:\/\/www.ebhor.com\/#organization"},"keywords":["constructor","destructor","php"],"articleSection":["php"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/","url":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/","name":"PHP Constructor and Destructor - Ebhor.com","isPartOf":{"@id":"https:\/\/www.ebhor.com\/#website"},"datePublished":"2018-10-09T11:00:55+00:00","dateModified":"2019-11-08T11:28:58+00:00","description":"PHP Constructor and Destructor with need definition explanation program and example. step by step description in easy way","breadcrumb":{"@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ebhor.com\/php-constructor-and-destructor\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ebhor.com\/php-constructor-and-destructor\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ebhor.com\/"},{"@type":"ListItem","position":2,"name":"PHP Constructor and Destructor"}]},{"@type":"WebSite","@id":"https:\/\/www.ebhor.com\/#website","url":"https:\/\/www.ebhor.com\/","name":"Ebhor.com","description":"Read The Latest Post Java, Css, Html, Php learning articles","publisher":{"@id":"https:\/\/www.ebhor.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ebhor.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.ebhor.com\/#organization","name":"Ebhor.com","url":"https:\/\/www.ebhor.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ebhor.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.ebhor.com\/wp-content\/uploads\/2021\/05\/cropped-cropped-cropped-ebhor_logo_100x.jpg","contentUrl":"https:\/\/www.ebhor.com\/wp-content\/uploads\/2021\/05\/cropped-cropped-cropped-ebhor_logo_100x.jpg","width":100,"height":100,"caption":"Ebhor.com"},"image":{"@id":"https:\/\/www.ebhor.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ebhorcom\/"]},{"@type":"Person","@id":"https:\/\/www.ebhor.com\/#\/schema\/person\/6a63908fdbbb27289970569ebd2e709b","name":"manish","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ebhor.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/def43b1d916cff76638fa6c47604a2bb?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/def43b1d916cff76638fa6c47604a2bb?s=96&r=g","caption":"manish"},"description":"A Learner and Teacher"}]}},"_links":{"self":[{"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/posts\/3891"}],"collection":[{"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/comments?post=3891"}],"version-history":[{"count":0,"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/posts\/3891\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/media?parent=3891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/categories?post=3891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ebhor.com\/wp-json\/wp\/v2\/tags?post=3891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}