여러분이 사용하고 계신 브라우저는 HTML5를 지원하지 않기 때문에 몇몇 요소가 제대로 보이도록 JScript를 사용하고 있습니다. 하지만 여러분의 브라우저 설정에서 스크립트 기능이 꺼져있으므로, 현재 페이지를 제대로 확인하시려면 스크립트 기능을 켜주셔야 합니다. PHP - 기초 - OOP 소멸자
PHP – 기초 – OOP 소멸자
2년전 작성
1년전 수정

__destruct 함수

PHP OOP 소멸자 에 대해 공부해보자.

소멸자는 객체가 소멸되거나 스크립트가 중지되거나 종료될 때 호출된다.

PHP 스크립트 끝에서 자동으로 이 __destruct() 함수를 호출한다.

destruct 함수는 두 개의 밑줄(__)로 시작한다.

생성자와 소멸자는 코드 양을 줄이는 데 도움이 되므로 매우 유용하다.

한 개의 속성
PHP
<?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");
?>
기본 예시
The fruit is Apple.
두 개의 속성
PHP
<?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");
?>
기본 예시
The fruit is Apple and the color is red.
참고

Mingg`s Diary
밍구
공부 목적 블로그