我对此采取了不同的方法,可能不是最好的,但它是对我有意义的最好的方法之一。然而,它确实涉及修改源代码,但实现起来非常简单。
我通常有一个恰当命名的初级班Core
<?php
require_once("{$_SERVER['DOCUMENT_ROOT']}\Matilda\class\Settings.class.php");
require_once("{$_SERVER['DOCUMENT_ROOT']}\Matilda\class\Auth.class.php");
require_once("{$_SERVER['DOCUMENT_ROOT']}\Matilda\class\Permissions.class.php");
require_once("{$_SERVER['DOCUMENT_ROOT']}\Matilda\class\Procedures.class.php");
require_once("{$_SERVER['DOCUMENT_ROOT']}\Matilda\class\Audit.class.php");
class Core {
/**
* @var External class
*/
public $Settings,$Sql,$Auth,$Permissions,$Payroll,$Customers,$Plans,$Billing,$Engin,$AAPT,$Stock,$Prospects,$Freeside,$Audit;
/**
* @var Base Config
*/
private $InitConfig = array(
'Payroll' => false,
'Customers' => false,
'Plans' => false,
'Billing' => false,
'Engin' => false,
'AAPT' => false,
'Stock' => false,
'Prospects' => false,
'Freeside' => false,
);
public function __construct($Config = array()) {
// Session instantiation
if (!isset($_SESSION)) session_start();
if (!is_array($Config)) die('Core instantiation parameter must be an array, even if its empty');
$this->InitConfig = array_merge($this->InitConfig, $Config);
// Base classes
$this->Settings = new Settings();
$this->Sql = new MySQLi(/* Mysql info */)
$this->Audit = new Audit($this->Settings, $this->Sql);
$this->Auth = new Auth($this->Settings, $this->Sql, $this->Audit);
$this->Permissions = new Permissions($this->Settings, $this->Sql, $this->Auth, $this->Audit);
// Optional class handling
foreach ($this->InitConfig as $Dependency => $State) {
if ($State == true) {
require_once($this->Settings->RootDir . "/class/{$Dependency}.class.php");
$this->$Dependency = new $Dependency($this->Settings, $this->Sql, $this->Auth, $this->Permissions, $this->Audit);
}
}
}
这几乎展示了我如何提供可选的加载配置。
它具有所需的 5 个默认类,因此无论如何都会加载:设置、身份验证、权限、程序和审核
然后我可以实例化可选类:$Core = new Core(array('Payroll' => true))
它将实例化Payroll.class.php
并可以通过$Core->Payroll
几乎这只是存储树级函数的好方法
$Core->Members->FetchMemberByID($ID);
$Core->Plans->CreateNewPlan($PlanName, $Monthly, $Attributes = array())
etc etc.
有些人可能会为此打我一巴掌,但我个人认为这是一种相对体面的方式来完成你想要的,尽管不是你通常喜欢的方法(不更改代码)。
还可以方便地为您正在制作的任何内容创建插件:)