博客列表 >2月13日作业

2月13日作业

-   迷舍人
- 迷舍人原创
2020年02月15日 19:37:49576浏览

自动加载器

代码

···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()">';
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议