PHP Destructor

1 min read

PHP – The __destruct Function #

A destructor is called when the object is destructed or the script is stopped or exited.

Объектыг устгах эсвэл скриптийг зогсоох эсвэл гарахад устгагчийг дууддаг.

If you create a __destruct() function, PHP will automatically call this function at the end of the script.

Хэрэв та __destruct() функцийг үүсгэсэн бол PHP скриптийн төгсгөлд энэ функцийг автоматаар дуудах болно.

Notice that the destruct function starts with two underscores (__)!

Устгах функц нь хоёр доогуур зураас (__) -аас эхэлдэг болохыг анхаарна уу!

The example below has a __construct() function that is automatically called when you create an object from a class, and a __destruct() function that is automatically called at the end of the script:

Доорх жишээнд ангиас объект үүсгэх үед автоматаар дуудагддаг __construct () функц, скриптийн төгсгөлд автоматаар дуудагддаг __destruct () функцууд байдаг.

Example #

Жишээ #

<?php class Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function __destruct() { echo "The fruit is {$this->name}."; } } $apple = new Fruit("Apple"); ?>

Another example:

Өөр нэгэн жишээ:

Example #

Жишээ #

<?php class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function __destruct() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } $apple = new Fruit("Apple", "red"); ?>

Tip: As constructors and destructors helps reducing the amount of code, they are very useful!

Зөвлөгөө: Байгуулагч, устгагч нь кодын хэмжээг багасгахад тусалдаг тул тэдгээр нь маш их хэрэгтэй!

Powered by BetterDocs

Leave a Reply