new关键字和依赖注入对比
# 依赖注入方式
class LivePlayService extends BaseService
{
protected $config;
protected $config2;
public function __construct(CCLive $service, CCLive $tabService)
{
$this->config = $service;
$this->config2 = $tabService;
}
}
// 记录脚本开始时间
$start_time = microtime(true);
// 模拟脚本执行的操作
/** @var CCLive $ccService */
$ccService = app(LivePlayService::class);
// 记录脚本结束时间
$end_time = microtime(true);
// 计算执行时间
$execution_time = $end_time - $start_time;
return "脚本执行时间: ".$execution_time." 秒";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
脚本执行时间: 0.007404088973999 秒
1
# new关键字方式
class LivePlayService extends BaseService
{
protected $config;
protected $config2;
public function __construct()
{
$this->config = new CCLive();
$this->config2 = new CCLive();;
}
}
// 记录脚本开始时间
$start_time = microtime(true);
$result=new LivePlayService();
// 记录脚本结束时间
$end_time = microtime(true);
// 计算执行时间
$execution_time = $end_time - $start_time;
return "脚本执行时间: ".$execution_time." 秒";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
脚本执行时间: 0.0065999031066895 秒
1
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54