varunsridharan/php-autoloader

2.6 2019-10-04 09:42 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:47 UTC


README

轻量级 PSR-4 PHP 自动加载类。

Latest Stable Version Total Downloads Latest Unstable Version License composer.lock available

安装

通过Composer是安装此扩展的首选方式。

要安装 PHP_Autoloader 库,只需

$ composer require varunsridharan/php-autoloader

前面的命令只会安装必要的文件,如果您想 下载整个源代码,可以使用

$ composer require varunsridharan/php-autoloader --prefer-source

您也可以使用 Git 克隆完整的仓库

$ git clone https://github.com/varunsridharan/php-autoloader.git

或者 手动安装

下载 autoloader.php:

$ wget https://raw.githubusercontent.com/varunsridharan/php-autoloader/master/src/autoloader.php

用法

参数

  • $namespace : 处理自动加载的 PHP 命名空间
  • $classmap :https://github.com/varunsridharan/php-classmap-generator 生成的类映射文件。
  • $path : 查找 php 文件的完整路径
  • $options : 一组有用的参数。
  • $prepend : 选项将当前自动加载程序添加到现有的自动加载程序之前/之后。

$options 参数

  array(
      /**
       * An Array of Class Namespace to Exclude While Checking for current namespace.
       * Eg
       * Main Namespace \Testing\Core
       * Exclude \Testing\Core\Abstracts
       */
      'exclude' => false,
      
      /**
       * Custom Option To Quickly Remap A Class File. 
       * If any class added then it will not search.
       * instead it gets the location from here and loads it.
       * An Array of class and its file location
       */
      'mapping' => array(),
      
      /**
       * Set To True / False.
       */
      'debug'   => false,
  );

示例文件夹结构

  | - src\
  | -- class1\
  | --- class1.php
  | -- class2\
  | --- class2.php
  | --- class3\
  | ---- class3.php
  | - loader.php
  | - index.php

loader.php 源代码

  $autoloader = new \Varunsridharan\PHP\Autoloader('varun',__DIR__.'/src/',array(
      'mapping' => array(
          'varun\class2\class3\class3' => 'src/class2/class3/class3.php',
      )
  ))

index.php 源代码

  require __DIR__.'loader.php';
  
  $class1 = new \varun\class1\class1(); // This file is autoloaded based on the namespace 
  $class2 = new \varun\class1\class2(); // This file is autoloaded based on the namespace 
  $class3 = new \varun\class1\class3(); // This file is loaded using the data from remap array

📝 变更日志

此项目的所有显著更改都将在此文件中记录。

格式基于 Keep a Changelog,并且此项目遵循 语义化版本控制

查看 CHANGELOG.md

🤝 贡献

如果您想帮忙,请查看 问题列表

📜 许可证 & 行为准则

📣 反馈

  • ⭐ 如果此项目对您有帮助! 😉
  • 如果您需要帮助/发现了一个错误,请创建一个 🔧 问题

💰 赞助者

I 在 2013 年爱上了开源,从那时起就没有回头路了!您可以在 这里 了解更多关于我的信息。如果您,或您的公司,使用了我的任何项目或喜欢我所做的事情,请考虑支持我。我打算长期投入。

  • ☕ 我们何不在咖啡上互相了解?只需 $9.99 就可以为我买一杯咖啡。
  • ☕️☕️ 您是否愿意每月为我买两杯咖啡?您只需 $9.99
  • 🔰 我们热爱改善开源项目。支持一次开源维护 1 小时,只需 $24.99
  • 🚀 您是否喜欢开源工具?我也是!支持一次开源开发 1 小时,只需 $49.99

联系 & 问候 👋

Varun Sridharan 用♥搭建