{"id":126,"date":"2026-05-20T11:07:53","date_gmt":"2026-05-20T11:07:53","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=126"},"modified":"2026-05-21T14:40:29","modified_gmt":"2026-05-21T14:40:29","slug":"constructors-and-destructors","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors","title":{"rendered":"Constructors and Destructors"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors and destructors are special functions in object-oriented programming. They are mainly used in programming languages such as C++ and PHP classes to manage objects automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor is called when an object is created, while a destructor is called when an object is destroyed or removed from memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding constructors and destructors helps developers write cleaner, more efficient, and organized programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand constructors and destructors<\/li>\n\n\n\n<li>Learn how constructors initialize objects<\/li>\n\n\n\n<li>Understand how destructors release resources<\/li>\n\n\n\n<li>Create classes using constructors and destructors<\/li>\n\n\n\n<li>Improve object-oriented programming skills<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor is a special method that automatically runs when an object of a class is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors are mainly used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initialize object properties<\/li>\n\n\n\n<li>Assign default values<\/li>\n\n\n\n<li>Execute setup tasks automatically<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Constructors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatically called during object creation<\/li>\n\n\n\n<li>Has the same name as the class in some languages<\/li>\n\n\n\n<li>Can accept parameters<\/li>\n\n\n\n<li>Helps reduce repetitive code<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor Example in PHP<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Student {<br><br>    public $name;<br><br>    function __construct($studentName) {<br>        $this-&gt;name = $studentName;<br>        echo \"Student Name: \" . $this-&gt;name;<br>    }<br>}<br><br>$obj = new Student(\"Ali\");<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Student Name: Ali<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor with Multiple Parameters<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Employee {<br><br>    public $name;<br>    public $salary;<br><br>    function __construct($empName, $empSalary) {<br>        $this-&gt;name = $empName;<br>        $this-&gt;salary = $empSalary;<br>    }<br><br>    function display() {<br>        echo $this-&gt;name;<br>        echo $this-&gt;salary;<br>    }<br>}<br><br>$emp = new Employee(\"Ahmed\", 50000);<br>$emp-&gt;display();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Constructors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Saves development time<\/li>\n\n\n\n<li>Automatically initializes data<\/li>\n\n\n\n<li>Improves code readability<\/li>\n\n\n\n<li>Reduces manual setup work<\/li>\n\n\n\n<li>Makes programs more organized<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Destructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A destructor is a special method that automatically runs when an object is destroyed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Destructors are used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Close database connections<\/li>\n\n\n\n<li>Release memory<\/li>\n\n\n\n<li>Remove temporary files<\/li>\n\n\n\n<li>Perform cleanup operations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Destructors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatically called at the end of an object\u2019s life<\/li>\n\n\n\n<li>Cannot accept parameters<\/li>\n\n\n\n<li>Used for cleanup tasks<\/li>\n\n\n\n<li>Helps manage resources efficiently<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Destructor Example in PHP<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Test {<br><br>    function __construct() {<br>        echo \"Object Created\";<br>    }<br><br>    function __destruct() {<br>        echo \"Object Destroyed\";<br>    }<br>}<br><br>$obj = new Test();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Object Created<br>Object Destroyed<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In web applications:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Constructors are used to connect to databases when objects are created<\/li>\n\n\n\n<li>Destructors are used to close database connections automatically<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This improves performance and resource management.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Constructor and Destructor<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Constructor<\/th><th>Destructor<\/th><\/tr><\/thead><tbody><tr><td>Called when object is created<\/td><td>Called when object is destroyed<\/td><\/tr><tr><td>Used for initialization<\/td><td>Used for cleanup<\/td><\/tr><tr><td>Can accept parameters<\/td><td>Cannot accept parameters<\/td><\/tr><tr><td>Executes at the start<\/td><td>Executes at the end<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use constructors to initialize important data<\/li>\n\n\n\n<li>Use destructors only for cleanup tasks<\/li>\n\n\n\n<li>Keep constructor logic simple<\/li>\n\n\n\n<li>Avoid unnecessary resource usage<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Constructors and Destructors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors and destructors are commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web development<\/li>\n\n\n\n<li>Database management systems<\/li>\n\n\n\n<li>Game development<\/li>\n\n\n\n<li>Software applications<\/li>\n\n\n\n<li>Enterprise systems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Career Benefits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learning constructors and destructors helps students become:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP Developers<\/li>\n\n\n\n<li>C++ Programmers<\/li>\n\n\n\n<li>Backend Developers<\/li>\n\n\n\n<li>Software Engineers<\/li>\n\n\n\n<li>Object-Oriented Programming Experts<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What constructors are<\/li>\n\n\n\n<li>What destructors are<\/li>\n\n\n\n<li>Differences between constructors and destructors<\/li>\n\n\n\n<li>Real-world applications<\/li>\n\n\n\n<li>Benefits of using constructors and destructors<\/li>\n\n\n\n<li>Example programs using classes and objects<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > Object-Oriented PHP > Constructors and Destructors<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779275278303\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":38,"template":"","class_list":["post-126","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constructors and Destructors - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructors and Destructors - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:40:29+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=constructors-and-destructors\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=constructors-and-destructors\",\"name\":\"Constructors and Destructors - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T11:07:53+00:00\",\"dateModified\":\"2026-05-21T14:40:29+00:00\",\"description\":\"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=constructors-and-destructors#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=constructors-and-destructors\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=constructors-and-destructors#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > Object-Oriented PHP > Constructors and Destructors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructors and Destructors - Learn PHP with GiGz.PK","description":"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.","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:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors","og_locale":"en_US","og_type":"article","og_title":"Constructors and Destructors - Learn PHP with GiGz.PK","og_description":"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.","og_url":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:40:29+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors","url":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors","name":"Constructors and Destructors - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T11:07:53+00:00","dateModified":"2026-05-21T14:40:29+00:00","description":"Learn constructors and destructors in PHP with simple examples, object initialization, cleanup methods, and OOP basics.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=constructors-and-destructors#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > Object-Oriented PHP > Constructors and Destructors"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}