批改状态:合格
老师批语:思路很清楚
(1)、view代码:
<?phpnamespace mvc;//输出显示类class Show{public function show($data){$table='<table>';$table.='<caption>员工信息表</caption>';$table.='<tr><th>ID</th><th>工号</th><th>名字</th><th>性别</th><th>职位</th><th>工龄</th></tr>';foreach ($data as $staff){$table.='<tr><td>'.$staff['id'].'</td><td>'.$staff['工号'].'</td><td>'.$staff['name'].'</td><td>'.$staff['sex'].'</td><td>'.$staff['position'].'</td><td>'.$staff['seniority'].'</td></tr>';}$table.='</table>';return $table;}}
(2)model代码
<?phpnamespace mvc;//定义获取数据类class Model{public function get_data(){return [['id'=>1,'工号'=>'001','name'=>'李总','sex'=>1,'position'=>'董事长','seniority'=>10],['id'=>2,'工号'=>'002','name'=>'小花','sex'=>0,'position'=>'财务会计','seniority'=>4],['id'=>3,'工号'=>'003','name'=>'小王','sex'=>1,'position'=>'后勤','seniority'=>3],['id'=>4,'工号'=>'004','name'=>'小七','sex'=>0,'position'=>'人事','seniority'=>7],['id'=>5,'工号'=>'005','name'=>'小八','sex'=>1,'position'=>'销售','seniority'=>3],];}}
(3)controls代码
<?phpnamespace mvc;//导入数据操作require 'model.php';//导入视图显示require 'view.php';class Controls{public $model;public $view;public function __construct($model,$view){$this->model=$model;$this->view=$view;}public function control(){$data=$this->model->get_data();return $this->view->show($data);}}$model=new Model();$view=new Show();$index=new Controls($model,$view);echo $index->control();#注释css样式echo '<style>table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}caption {font-size: 1.2rem; margin-bottom: 10px;}tr:first-of-type { background-color:lightblue;}td,th {border: 1px solid}td:first-of-type {text-align: center}</style>';

1、关于类的相关知识点:
extends:继承 例:B类继承了A类
class A {}class B extends A {}
2、封装:类内方法和属性使用声明:
(1)public:公共的,类内部和外部都可以直接调用,包含子类
(2)private:私有的,仅类的内部可以直接调用
(3)protected:保护的,不允许类外部使用,但子类可以调用
3、静态变量:static 类内外都可以使用:self::$static/ClassName::$static
可以直接使用类名直接访问(不需要实列化):ClassName::$static
静态方法(声明的静态函数)不能用实例访问,内部不允许使用$this
4、接口:interface 实现接口的类, 必须将接口中声明的方法全部实现
使用接口的类关键字:implements
例:
<?phpinterface iDome{public function get_info();public function get_data();}class Demo implements iDome{public $name;protected $age;public function __construct($name,$age){$this->name=$name;$this->age=$age;}public function get_info(){return $this->name.'的年龄'.$this->age;}public function get_data(){return [$this->name,$this->age];}}$a=new Demo('ldy',27);echo $a->name,'<br>';echo $a->get_info(),'<br>';echo print_r($a->get_data(),true);

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号