Blogger Information
Blog 18
fans 0
comment 0
visits 10873
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2月13日作业
- 迷舍人
Original
585 people have browsed it

自动加载器

代码

···php
<?php

namespace chapter6;

spl_autoload_register(function ($class) {
// 设置项目前缀
$prefix = ‘App\edu\‘;

  1. // 设置具有项目前缀的类名所对应的类的基目录
  2. $base_dir = __DIR__ . '/src/';
  3. // 去掉项目前缀,获取真实的类名称
  4. $real_class = substr($class, strlen($prefix));
  5. // die($real_class);
  6. // 将命名空间分隔符,替换成目录分隔符
  7. $path = str_replace(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $real_class);
  8. // 加上基目录和php的后缀'.php'
  9. $file = $base_dir . $path . '.php';
  10. // die($file);
  11. // 查看文件是否存在
  12. var_dump(file_exists($file));
  13. file_exists($file) ? require $file : die('文件不存在');

});

  1. ```php
  2. <?php
  3. namespace src\home;
  4. class User
  5. {
  6. public static function test(): string
  7. {
  8. return __CLASS__;
  9. }
  10. }
  11. // echo User::class;die;
  12. die(User::test());

composer和下载组件使用

  1. <?php
  2. namespace chapter6;
  3. use App\edu\home\User;
  4. require 'demo2.php';
  5. User::abc();
  1. <?php
  2. namespace chapter6;
  3. // 验证码
  4. // PSR-4的自动加载器
  5. require __DIR__ . '/vendor/autoload.php';
  6. //导入验证码组件,空间别名
  7. use Gregwar\Captcha\CaptchaBuilder;
  8. // 实例化验证码类
  9. $builder = new CaptchaBuilder;
  10. // 生成验证码
  11. $builder->build();
  12. //生成内联验证码 ,可以放在内联标签:base64
  13. $captcha = $builder->inline();
  14. echo '<img src="'.$captcha.'" onclick="location.reload()">';
Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

Teacher's comments:看清作业要求
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post