内部で使用するための独自のフレームワークを構築しようとしています。私はこのような構造を得ました:
index.php
boot /
booter.php
application /
controllers /
indexcontroller.php
core /
template.class.php
model.class.php
controller.class.php
cache /
memcached.php
something /
something.php
Booter.php には以下が含まれます: (現在、コア ディレクトリにあるファイルのみで動作しています):
class Booter
{
private static $controller_path, $model_path, $class_path;
public static function setDirs($controller_path = 'application/controllers', $model_path = 'application/models', $classes_path = 'core')
{
self::$controller_path = $controller_path;
self::$model_path = $model_path;
self::$class_path = $classes_path;
spl_autoload_register(array('Booter', 'LoadClass'));
if ( DEBUG )
Debugger::log('Setting dirs...');
}
protected static function LoadClass($className)
{
$className = strtolower($className);
if ( file_exists(DIR . '/' . self::$model_path . '/' . $className . '.php') )
{
require(DIR . '/' . self::$model_path . '/' . $className . '.php');
}
else if ( file_exists(DIR . '/' . self::$class_path . '/' . $className . '.class.php') )
{
require(DIR . '/' . self::$class_path . '/' . $className . '.class.php');
}
else if ( file_exists(DIR . '/' . self::$controller_path . '/' . $className . '.php') )
{
require(DIR . '/' . self::$controller_path . '/' . $className . '.php');
}
if ( DEBUG )
Debugger::log('AutoLoading classname: '.$className);
}
}
私のアプリケーション/コントローラ/インデックスコントローラは次のようになります:
<?
class IndexController extends Controller
{
public function ActionIndex()
{
$a = new Model; // It works
$a = new Controller; //It works too
}
}
?>
そして、ここに私の質問があります:
[質問1]
私のコードは現在次のように動作しています:
$a = new Model; // Class Model gets included from core/model.class.php
名前空間を持つクラスによってインクルード ファイルを実装するにはどうすればよいですか? 例えば:
$a = new Cache\Memcached; // I would like to include file from /core/CACHE/Memcached.php
$a = new AnotherNS\smth; // i would like to include file from /core/AnotherNS/smth.php
等々。名前空間の処理を作成するにはどうすればよいですか?
[質問2]
クラス、コントローラー、およびモデルに単一の自動ロードを使用することは良い習慣ですか、それとも 3 つの異なるメソッドで 3 つの異なる spl_autoload_register を定義する必要がありますか?その理由は?