onlinecode

Destructor in PHP OOP – onlinecode

Destructor in PHP OOP – onlinecode

In this post we will give you information about Destructor in PHP OOP – onlinecode. Hear we will give you detail about Destructor in PHP OOP – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Destructor in PHP :

Destructor in PHP is similar to other oops(object oriented programming) concept.

PHP Destructor is handled by the Garbage Collector which always focus over object when there are no needed of object then it automatically destroyed.

Destructor function are inverse of Constructor. The method name of Destructor in php is differ from Constructor method name and it can’t take any arguments like constructor.

Destructor function name in PHP is ‘__destruct()’.

Constructor is involved when objects are created and Desctructor is involved when object are deleted.


Here is a example of destructor :

  1. class Bird
  2. {
  3. public $bird_name="No any birds for now";
  4. public function__construct($bird_name)
  5. {
  6. echo"I'm alive!";
  7. $this--->bird_name =$bird_name;
  8. }
  9. public function__destruct()
  10. {
  11. echo"I'm dead now :(";
  12. }
  13. }
  14. $bird=newBird("Sparrow is bird");
  15. echo"Name of the bird: ".$bird->bird_name;
  16. ?>
  17. Output :
  18. I'm alive!Name of the bird: Sparrow is birdI'm dead now :(
  19. // so as we seen, __destruct is called when object is removed or destroyed.
class Bird{    public $bird_name = "No any birds for now";        public function __construct($bird_name)    {        echo "I'm alive!";            $this--->bird_name = $bird_name;    }        public function __destruct()    {        echo "I'm dead now :(";    }}$bird = new Bird("Sparrow is bird");echo "Name of the bird: " . $bird->bird_name;?>Output :I'm alive!Name of the bird: Sparrow is birdI'm dead now :(// so as we seen, __destruct is called when object is removed or destroyed.

Label :

PHP

OOP

Object Oriented Programming

Hope this code and post will helped you for implement Destructor in PHP OOP – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version