Context::hasLocal
(PHP 8.6+, True Async 1.0)
public Context::hasLocal(string|object $key): bool
Перевіряє, чи існує значення із зазначеним ключем лише в поточному (локальному) контексті.
На відміну від has(), цей метод не шукає в батьківських контекстах.
Параметри
- key
- Ключ для перевірки. Може бути рядком або об’єктом.
Значення, що повертаються
true, якщо ключ знайдено в локальному контексті, false в іншому випадку.
Приклади
Приклад #1 Різниця між has і hasLocal
<?php
use function Async\current_context;
use function Async\spawn;
current_context()->set('inherited_key', 'value');
spawn(function() {
current_context()->set('local_key', 'value');
// has() шукає вгору по ієрархії
var_dump(current_context()->has('inherited_key')); // true
var_dump(current_context()->has('local_key')); // true
// hasLocal() перевіряє лише поточний рівень
var_dump(current_context()->hasLocal('inherited_key')); // false
var_dump(current_context()->hasLocal('local_key')); // true
});
Приклад #2 Перевірка з ключем-об’єктом
<?php
use function Async\current_context;
use function Async\spawn;
$configKey = new stdClass();
current_context()->set($configKey, ['debug' => true]);
spawn(function() use ($configKey) {
$localKey = new stdClass();
current_context()->set($localKey, 'local');
var_dump(current_context()->hasLocal($configKey)); // false
var_dump(current_context()->hasLocal($localKey)); // true
});
Приклад #3 Умовна ініціалізація локального значення
<?php
use function Async\current_context;
use function Async\spawn;
spawn(function() {
// Ініціалізуємо значення лише якщо воно не встановлено локально
if (!current_context()->hasLocal('request_count')) {
current_context()->set('request_count', 0);
}
echo current_context()->getLocal('request_count') . "\n"; // 0
});
Дивіться також
- Context::has — Перевірка з обходом ієрархії
- Context::findLocal — Знайти значення в локальному контексті
- Context::getLocal — Отримати локальне значення (кидає виключення)