mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
Added document. (#2119)
This commit is contained in:
parent
75b9384996
commit
deeeef0ec7
@ -1,5 +1,82 @@
|
||||
# 常见问题
|
||||
|
||||
## `Inject` 或 `Value` 注解不生效
|
||||
|
||||
`2.0` 使用了构造函数中注入 `Inject` 和 `Value` 的功能,以下两种场景,可能会导致注入失效,请注意使用。
|
||||
|
||||
1. 原类没有使用 `Inject` 或 `Value`,但父类使用了 `Inject` 或 `Value`,且原类写了构造函数,同时又没有调用父类构造函数的情况。
|
||||
|
||||
这样就会导致原类不会生成代理类,而实例化的时候又调用了自身的构造函数,故没办法执行到父类的构造函数。
|
||||
所以父类代理类中的方法 `__handlePropertyHandler` 就不会执行,那么 `Inject` 或 `Value` 注解就不会生效。
|
||||
|
||||
```php
|
||||
class ParentClass {
|
||||
/**
|
||||
* @Inject
|
||||
* @var Service
|
||||
*/
|
||||
protected $value;
|
||||
}
|
||||
|
||||
class Origin extends ParentClass
|
||||
{
|
||||
public function __construct() {}
|
||||
}
|
||||
```
|
||||
|
||||
2. 原类没有使用 `Inject` 或 `Value`,但 `Trait` 中使用了 `Inject` 或 `Value`。
|
||||
|
||||
这样就会导致原类不会生成代理类,故没办法执行构造函数里的 `__handlePropertyHandler`,所以 `Trait` 的 `Inject` 或 `Value` 注解就不会生效。
|
||||
|
||||
```php
|
||||
trait OriginTrait {
|
||||
/**
|
||||
* @Inject
|
||||
* @var Service
|
||||
*/
|
||||
protected $value;
|
||||
}
|
||||
|
||||
class Origin
|
||||
{
|
||||
use OriginTrait;
|
||||
}
|
||||
```
|
||||
|
||||
基于上述两种情况,可见 `原类` 是否生成代理类至关重要,所以,如果使用了带有 `Inject` 或 `Value` 的 `Trait` 和 `父类` 时,给原类添加一个 `Inject`,即可解决上述两种情况。
|
||||
|
||||
```php
|
||||
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
|
||||
trait OriginTrait {
|
||||
/**
|
||||
* @Inject
|
||||
* @var Service
|
||||
*/
|
||||
protected $trait;
|
||||
}
|
||||
|
||||
class ParentClass {
|
||||
/**
|
||||
* @Inject
|
||||
* @var Service
|
||||
*/
|
||||
protected $value;
|
||||
}
|
||||
|
||||
class Origin extends ParentClass
|
||||
{
|
||||
use OriginTrait;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var StdoutLoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
}
|
||||
```
|
||||
|
||||
## Swoole 短名未关闭
|
||||
|
||||
```
|
||||
|
@ -256,6 +256,7 @@ class Scanner
|
||||
$aspects = array_merge($providerConfig['aspects'], $baseConfig['aspects'], $aspects);
|
||||
|
||||
[$removed, $changed] = $this->getChangedAspects($aspects, $lastCacheModified);
|
||||
// When the aspect removed from config, it should removed from AspectCollector.
|
||||
foreach ($removed as $aspect) {
|
||||
AspectCollector::clear($aspect);
|
||||
}
|
||||
@ -294,7 +295,6 @@ class Scanner
|
||||
// Priority
|
||||
$priority = $priority ?: ($instancePriority ?? null);
|
||||
// Save the metadata to AspectCollector
|
||||
// TODO: When the aspect removed from config, it should removed from AspectCollector.
|
||||
AspectCollector::setAround($aspect, $classes, $annotations, $priority);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user