我有一个名为EQ
的主类,已连接到其他类,可以在此GitHub link中进行查看。
EQ类未连接到我的composer,我使用以下命令在本地服务器中调用它:
php -f path/to/EQ.php
和使用CRON作业的实时服务器:
1,15,30,45 * * * * (sleep 12; /usr/bin/php -q /path/to/EQ.php >/dev/null 2>&1)
我不确定如何正确使用自动加载器并将所有相关文件加载到此类,并删除require_once
。我已经尝试过了,而且看起来确实可行:
spl_autoload_register(array('EQ', 'autoload'));
如何解决此问题?
//Creates a JSON for all equities // iextrading API
require_once __DIR__ . "/EquityRecords.php";
// Gets data from sectors // iextrading API
require_once __DIR__ . "/SectorMovers.php";
// Basic Statistical Methods
require_once __DIR__ . "/ST.php";
// HTML view PHP
require_once __DIR__ . "/BuildHTMLstringForEQ.php";
// Chart calculations
require_once __DIR__ . "/ChartEQ.php";
// Helper methods
require_once __DIR__ . "/HelperEQ.php";
if (EQ::isLocalServer()) {
error_reporting(E_ALL);
} else {
error_reporting(0);
}
/**
* This is the main method of this class.
* Collects/Processes/Writes on ~8K-10K MD files (meta tags and HTML) for equities extracted from API 1 at iextrading
* Updates all equities files in the front symbol directory at $dir
*/
EQ::getEquilibriums(new EQ());
/**
* This is a key class for processing all equities including two other classes
* Stock
*/
class EQ
{
}
spl_autoload_register(array('EQ', 'autoload'));
答案 0 :(得分:1)
基本上,您的自动加载器功能会将类名映射到文件名。例如:
LC_ALL=C who -u
但是,大多数这些东西内置在PHP中,使您的代码更加简单:
class EQ
{
public function autoloader($classname)
{
$filename = __DIR__ . "/includes/$classname.class.php";
if (file_exists($filename)) {
require_once $filename;
} else {
throw new Exception(sprintf("File %s not found!", $filename));
}
}
}
spl_autoload_register(["EQ", "autoloader"]);
$st = new ST;
// ST.php should be loaded automatically
$st->doStuff();
只要spl_autoload_extensions(".php");
spl_autoload_register();
$st = new ST;
$st->doStuff();
在您的ST.php
中的任何地方,它就可以工作。不需要自动加载器功能。