{"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 The c No need to call it by an object. Simply constructor is a function which called when Constructor declares by two ways.<\/p>\n\n\n\n Only one constructor creates in one class. It is used to run Like Database Connection. The constructor<\/p>\n\n\n\n Example:<\/strong><\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Explanation 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 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 Destructor defines by __destruct keyword. Example:<\/strong><\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n 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":"\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
\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
Destructor<\/strong><\/h2>\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
I'm alive! Name of the animal: Bob\nI\u2019m dead now \uf04c\n\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"