<?php

namespace Spiral\Core\Internal\Common;

/**
 * @internal
 */
trait DestructorTrait
{
    /**
     * @return void
     */
    public function destruct()
    {
        $class = new \ReflectionClass($this);
        foreach ($class->getProperties() as $property) {
            $name = $property->getName();
            if (!isset($this->$name)) {
                continue;
            }
            $value = $this->$name;
            unset($this->$name);
            if (\is_object($value) && \method_exists($value, 'destruct')) {
                $value->destruct();
            }
        }
    }
}
