Update important.md (#4788)

This commit is contained in:
李铭昕 2022-05-28 08:38:31 +08:00 committed by GitHub
parent 70ea79fe53
commit 4577f344ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,3 +26,59 @@ composer dump-autoload -o
# 生成代理类和注解缓存
php bin/hyperf.php
```
## 避免在魔术方法中切换协程
> __call __callStatic 除外
尽量避免在 `__get` `__set``__isset` 中切换协程,因为可能会出现不符合预期的情况
```php
<?php
require_once 'vendor/autoload.php';
Swoole\Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]);
class Foo
{
public function __get(string $name)
{
sleep(1);
return $name;
}
public function __set(string $name, mixed $value)
{
sleep(1);
var_dump($name, $value);
}
public function __isset(string $name): bool
{
sleep(1);
var_dump($name);
return true;
}
}
$foo = new Foo();
go(static function () use ($foo) {
var_dump(isset($foo->xxx));
});
go(static function () use ($foo) {
var_dump(isset($foo->xxx));
});
\Swoole\Event::wait();
```
当我们执行上述代码时,会返回以下结果
```shell
bool(false)
string(3) "xxx"
bool(true
```