Возможно ли, что __destruct вызывается до завершения __construct?

У меня есть проект привязь и в службе JSON-RPC я пишу в файл в __destruct, но иногда файл конфигурации очищается (это выглядит так {"sessions":[],"users":[]}) это происходит время от времени, а также, когда я быстро обновляю страницу до завершения запроса ajax, похоже, что __constructor не завершился до вызова __destruct, у меня есть такой код:

class Service {
protected $config_file;
protected $config;
const password_hash = 'h'; // function use for password on installation
const password_regex = '/([A-Za-z_][A-Za-z0-9_]*):(.*)/';

function __construct($config_file, $path) {
$this->path = $path;
$this->config_file = $config_file;
$full_path = $path . "/" . $this->config_file;
if (file_exists($full_path)) {
try {
$this->config = json_decode(file_get_contents($full_path));
} catch (Exception $e) {
$this->config = new stdClass();
}
// it had no write permission when first created while testing
if (!is_writable($full_path)) {
chmod($full_path, 0664);
}
} else {
$this->config = new stdClass();
}
if (!isset($this->config->sessions) || !is_array($this->config->sessions)) {
$this->config->sessions = array();
} else {
$this->config->sessions = array_map(function($session) {
return Session::cast($session);
}, array_filter($this->config->sessions, function($session){
return isset($session->token) && isset($session->username);
}));
}
if (!isset($this->config->users) || !is_array($this->config->sessions)) {
$this->config->users = array();
}
}
// ------------------------------------------------------------------------
function __destruct() {
$path = $this->path . "/" . $this->config_file;
$this->__write($path, json_encode($this->config));
}

У меня есть другие методы, которые изменяют объект конфигурации, как login что добавить новую сессию $this->config->sessions массив. Вот полный файл

Я пытался добавить флаг $this->corrupted это установлено в true, когда я получаю исключение, но это не решает проблему, это должно быть что-то со временем __destruct будучи призванным

Я также пытаюсь добавить флаг $this->safe_to_save это установлено в true в последней строке конструктора, но это также не сработало. В чем может быть проблема?

2

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector