Coding Class

Quizgum : destruct

Using Destructors

The destructor works when all the code in PHP is used.

How to create a destructor

function __destruct(){}

There are two underscores(_) in front of the destruct above.
So let's do it. Let's use the Car class we used earlier.
Should you turn off the engine when you have used up your car?

    class Car
        public $wheels;
        public $doors = 4;
        protected $color = 4;
        private $size;
        private $company;

        function __construct()
            echo "I started to run my car.";

        function __destruct()
            echo "<br>Turn off the car.";

        public function run()
            return "The car is running.";

        protected function stop()
            return "car stops.";

        protected function turn()
            return "turnning car";
    $honda = new Car;

Result of the code above

In the code above, I didn't call any methods, I just created an instance.
The constructor works automatically when you instantiate the instance, and the destructor works when the script in PHP is finished.
The Aberdevel Coding exercise book is in between the commercials of PHP 200. If you run it in a different environment, it is normal for the ads not to appear. ^^