現在、基本接続クラスにヘルパーを登録するための手動メソッドがあります。これは、次のようになります。
class db_con
{
// define the usual suspect properties..
public $helpers; // helper objects will get registered here..
public function __construct()
{
// fire up the connection or die trying
$this->helpers = (object) array();
}
public function __destruct()
{
$this->helpers = null;
$this->connection = null;
}
// $name = desired handle for the helper
// $helper = name of class to be registered
public function register_helper($name, $helper)
{
if(!isset($this->helpers->$name, $helper))
{
// tack on a helper..
$this->helpers->$name = new $helper($this);
}
}
// generic DB interaction methods follow..
}
次に、などのヘルパークラス。
class user_auth
{
public function __construct($connection){ }
public function __destruct(){ }
public function user_method($somevars)
{
// do something with user details
}
}
したがって、$connection
オブジェクトを作成した後、次のようなヘルパーを手動で登録します。
$connection->register_helper('users', 'user_auth');
今私の質問は、どういうわけか基本接続クラス内にヘルパークラスを自動ロードできますか?(register_helper()
メソッド内または同様のもの)または、手動でロードするか、何らかの形式の外部オートローダーを介してロードすることに制限されていますか?
この質問が他の場所で回答された場合はお詫びしますが、私はそれを見つけられず(試行不足のためではありません)、まだ何も自動ロードした経験がありません。
事前に感謝します、どんな助けやポインタも大歓迎です!:)
編集: Vicの提案によると、これは私が登録方法のために思いついた実用的な解決策です。
public function register_handlers()
{
$handler_dir = 'path/to/database/handlers/';
foreach (glob($handler_dir . '*.class.php') as $handler_file)
{
$handler_bits = explode('.', basename($handler_file));
$handler = $handler_bits[0];
if(!class_exists($handler, false))
{
include_once $handler_file;
if(!isset($this->handle->$handler, $handler))
{
$this->handle->$handler = new $handler($this);
}
}
}
}
これには、今のところオブジェクトが完全に含まれ、登録されているように見えます。このソリューションが「優れた」ソリューションであるかどうかにかかわらず、これ以上の入力またはテストなしではわかりません。