批改状态:合格
老师批语:
今日作业:
描述控制器与请求对象的功能与用途
控制器接收用户的请求,计算处理后返回给用户。请求对象用来接收用户的请求数据。给控制器使用
实例讲解一下:依赖注入的实现原理(构造器与普通方法)
<?php
namespace app\index\api;
//学生类
class Student
{
public function study(){
return '我在学校学习知识';
}
}
//教师类
class Teacher
{
public function teach(){
return '我在学校教书';
}
}
//学校类
class School
{
public function __construct(Student $student, Teacher $teacher)
{
$this->student = $student;
$this->teacher = $teacher;
}
}
//Test类
class Test extends School
{
public function tell()
{
$data[] = $this->student->study();
$data[] = $this->teacher->teach();
halt($data);
}
}点击 "运行实例" 按钮查看在线实例

以上为通过构造器的方法实现类的依赖注入,并且通过这个类的继承或实例化,可以直接调用其他类的方法而不用使用new的方式。像是直接使用一个类代理了多个类的方法。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号