__autoload()
を使用するか、必要なときに必要なファイルをロードするオブジェクトファクトリを作成できます。
あなたのライブラリファイルの範囲に問題がある場合は、おそらくレイアウトをリファクタリングする必要があります。ほとんどのライブラリは、どのスコープでもインスタンス化できるクラスのセットです。
以下は、非常に基本的なオブジェクトファクトリの例です。
class ObjectFactory {
protected $LibraryPath;
function __construct($LibraryPath) {
$this->LibraryPath = $LibraryPath;
}
public function NewObject($Name, $Parameters = array()) {
if (!class_exists($Name) && !$this->LoadClass($Name))
die('Library File `'.$this->LibraryPath.'/'.$Name.'.Class.php` not found.');
return new $Name($this, $Parameters);
}
public function LoadClass($Name) {
$File = $this->LibraryPath.'/'.$Name.'.Class.php'; // Make your own structure.
if (file_exists($File))
return include($File);
else return false;
}
}
// All of your library files should have access to the factory
class LibraryFile {
protected $Factory;
function __construct(&$Factory, $Parameters) {
$this->Factory = $Factory;
}
}
どこにでもクラスライブラリの設定に関するヒントを教えてくれるチュートリアルはありますか? – johnnietheblack
サンプルのオブジェクトファクトリを追加しました。 – matpie