hyperf 遇到的情况总结
楼主已经转go,不再更新
hyperf踩过的坑
协程
变量
public const const = 1000;
public static $static;
public function setvar()
{
$this->var = 'var';
self::$static = 'static';
Context::set('testvar', '1000');
return [
'context' => Context::get('testvar'),
];
}
public function getvar()
{
return [
'var' => $this->var,
'static' => self::$static,
'const' => self::const,
'context' => Context::get('testvar', 'no'),
];
}
像这种,全局定义的变量,静态属性,是所有协程共享的
但是,协程上下文,是协程隔离的
注解注入坑
突然报:must not be accessed before initialization
/**
* @Inject
* @var SourceTypeNewService
*/
private $sourceTypeNewService;
- runtime 删掉重起就行
启动
启动没扫描文件
遇到一个坑,很坑,深坑,start启动后不扫描文件,然后导致最新的文件没没改变。
注意!注意!注意!
查看是否,继承类出了问题
hyperf 因为协程,类实例化最好还是用,make()
报没有input()
这通常是,runtime文件没有更新,或者git分支切换。
可以删掉runtime重新启动
内存常驻
构造函数实例化类bug
public function __construct()
{
if ($this->sourceTypeNewService === null || $this->base === null) {
$this->sourceTypeNewService = new SourceTypeNewService();
$this->base = new Base();
}
}
像这样,构造函数,实例化类,在php-fpm模式下,每次都会断开,然后重新连接,执行构造函数,拿到实例化。
但是,swoole是常驻内存,不会断开,所以$this -> model 使用类,会很容易有变量污染。
- 错误,swoole worker工作模式,也是类似fpm 也是会每次都链接
继承
为了测试,继承是否和变量一样会有污染,所以做了一下代码测试
父类:trytest
<?php
namespace App\Controller;
class trytest
{
public static function index(){
}
public static function indexWithDate($date=''):string{}
public function getIndex(){
$data = 'nothing';
if (static::index()){
$data = static::index();
}
if (static::indexWithDate()){
$data = static::indexWithDate();
}
return [
'msg'=>$data,
'code'=>333
];
}
}
两个测试子类
testa:
<?php
namespace App\Controller;
class testa extends trytest
{
public static function indexWithDate($date = ''): string
{
return 'this is testa';
}
public function get(){
return $this->getIndex();
}
}
返回:
{
"msg": "this is testa",
"code": 333
}
testb:
<?php
namespace App\Controller;
class testb extends trytest
{
public static function indexWithDate($date = ''): string
{
return 'this is testbbbbb';
}
public function get(){
return $this->getIndex();
}
}
返回:
{
"msg": "this is testbbbbb",
"code": 333
}
由此可见,继承是隔离的,不会有污染,想想也是。。。。