我是PHP新手,不太熟悉使用git。如何在php中使用自動加載器
我得到了這個庫:
https://github.com/CKOTech/checkout-php-library
,我想在這裏運行示例代碼:
https://github.com/CKOTech/checkout-php-library/wiki/Tokens
我知道代碼可能不適合你很好地工作,因爲你需要但是,我不需要像「無法找到類ApiClient」的一般錯誤
我所做的只是簡單地在我的index.php文件中包含自動加載器,這就是我使用自動加載器所需做的一切嗎?它是否必須對composer.json做任何事情?
非常感謝您的幫助。
Autoloader.php:
<?php
function autoload($className)
{
$baseDir = __DIR__;
$realClassName = ltrim($className, '\\');
$realClassName = str_replace('\\',DIRECTORY_SEPARATOR,$realClassName);
$fileName = '';
$includePaths = $baseDir.DIRECTORY_SEPARATOR.$realClassName. '.php';
if ($file = stream_resolve_include_path($includePaths)) {
if (file_exists($file)) {
require $file;
}
}elseif(preg_match('/^\\\?test/', $className)) {
$fileName = preg_replace('/^\\\?test\\\/', '', $fileName);
$fileName = 'test' . DIRECTORY_SEPARATOR . $fileName;
include $fileName;
} else {
$classNameArray = explode('_', $className);
$includePath = get_include_path();
set_include_path($includePath);
if (!empty($classNameArray) && sizeof($classNameArray) > 1) {
if (!class_exists('com\checkout\packages\Autoloader')) {
include 'com'.DIRECTORY_SEPARATOR.'checkout'.DIRECTORY_SEPARATOR.'packages'.DIRECTORY_SEPARATOR.'Autoloader.php';
}
}
}
}
spl_autoload_register('autoload');