anoncom blog
  • ホーム
  • About
KEEP IN TOUCH

簡易クラスローダ

2月23
2009
Leave a Comment Written by anon
このエントリーをはてなブックマークに追加
はてなブックマーク - 簡易クラスローダ
Share on LinkedIn

車輪の再発明になっている可能性大な気がしますが、PHP4で動作する、動的な簡易クラスローダを作成してみました。

大まかにはZend FrameworkのZend_Loaderを参考にしていますが、基本的に1から書き直していています。

[php]
< ?php
/**
* 簡易クラスローダ
* for PHP4
*
* @author anon [email protected]>
*/
class ClassLoader
{
function ClassLoader() {}

/**
* 指定されたクラスを読み込み、インスタンスを返します。
*
* @param string $class クラス名
* @param string|NULL クラスディレクトリ
* @param boolean $once requireでファイルを読み込む際に、require_onceで読み込むか否か
* @param array|NULL $param インスタンス生成時に引き渡す引数
* @param string|NULL $loaderMethod インスタンス生成時に呼び出される、コンストラクタ以外のメソッド名(Singletonパターンでの呼び出しの場合など)
*/
function loadClass($class, $dir = NULL, $once = TRUE, $params = NULL, $loaderMethod = NULL)
{
if(is_null($dir)){
$dirs = explode(':', ini_get('include_path'));
$classfile_exists = false;
foreach($dirs as $dir){
$filepath = $dir . '/' . $class . '.php';
if(Froute_Loader::isFileAvailable($filepath)){
$classfile_exists = true;
break;
}

}
if(!$classfile_exists){
trigger_error('could not load class, causes class file"' . $class . '" is not found.', E_USER_WARNING);
}
}else{
$filepath = $dir . '/' . $class . '.php';
if(!ClassLoader::isFileAvailable($filepath)){
trigger_error('could not load class, causes class file "' . $filepath . '" is not found.', E_USER_ERROR);
}
}

if($once){
require_once $filepath;
}else{
require $filepath;
}

if(!class_exists($class)){
trigger_error('could not load class, causes undefined class name "' . $class . '".', E_USER_ERROR);
}

if(!is_null($loaderMethod)){
if(!method_exists($loaderMethod)){
trigger_error('could not load class, causes undefined function name "' . $class . '".', E_USER_ERROR);
}
if(is_null($params)){
$instance = call_user_func(array($class, $loaderMethod));
}else{
$instance = call_user_func(array($class, $loaderMethod), $params);
}
}else{
/*
if(!function_exists($class)){
trigger_error('could not load class, \'causes undefined function name "' . $class . '".', E_USER_ERROR);
}
*/
if(is_null($params)){
$instance = new $class;
}else{
//$instance = new call_user_func($class, $params); // to use php5
$instance = new $class($params); // to use php4
}
}
return $instance;
}

/**
* ファイルが存在するか確認します
*
* @param string $filename
* @return bool
*/
function isFileAvailable($filename)
{
if(file_exists($filename) && is_readable($filename)){
return true;
}else{
return false;
}
}
}
[/php]


使い方は以下の通り



[php]
< ?php
$class = ClassLoader::loadClass('SampleClass');
/*
以下の動作と同じ
require('SampleClass.php');
$class = new SampleClass();
*/
[/php]


[php]
< ?php

// クラスメソッドに渡す引数
$args = array($foo, $bar, $bazz);
$class = ClassLoader::loadClass('SampleClass', dirname(__FILE__) . '/classes', true, $args, 'getInstance');
/*
以下の動作と同じ
require_once(dirname(__FILE__) . '/classes/SampleClass.php');
$class = SampleClass::getInstance($foo, $bar, $bazz);
*/
[/php]

Related posts:

  1. 接続先のサーバが応答するか判別する
  2. PHPでの簡易文字列の一致の判定
Posted in 無分類 - Tagged PHP
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« 接続先のサーバが応答するか判別する
» Genesis Lightning Talks #13 に行って…

No Comments Yet

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

中の人

名前: あのん (anon) anon
詳細

 

2009年2月
日 月 火 水 木 金 土
« 1月   3月 »
1234567
891011121314
15161718192021
22232425262728

カテゴリー

  • Android
  • iPhone
  • Java
  • Linux
  • Mac
  • PC
  • PHP
  • Twitter
  • VOCALOID
  • イベント
  • サーバ
  • ソフトウェア
  • ネタ
  • ネット
  • モバイル
  • 無分類
  • 話題
  • 開発

アーカイブ

  • 2011年12月
  • 2011年9月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2011年1月
  • 2010年10月
  • 2010年8月
  • 2010年7月
  • 2010年6月
  • 2010年5月
  • 2010年3月
  • 2010年1月
  • 2009年11月
  • 2009年10月
  • 2009年8月
  • 2009年6月
  • 2009年4月
  • 2009年3月
  • 2009年2月
  • 2009年1月
  • 2008年12月
  • 2008年11月
  • 2008年7月
  • 2008年4月

最近の投稿

  • Galaxy Nexusを購入しました
  • さくらのVPSを使いながら行ったウェブサイトの3つの負荷対策
  • PSNの障害と個人情報漏洩について
  • ミクパ♪(初音ミク ライブパーティ 2011)に行ってきたよ
  • FirefoxからChromeに乗り換えてみた

最近のコメント

  • Twitterアカウント名が商標権侵害で訴えられそうになった話 に 2011年2月のこれだけ読めば分かる Web 制作者向け情報まとめ | ウェブル より
  • Twitterアカウント名が商標権侵害で訴えられそうになった話 に 2/1 (火) 前田敦子 豊胸疑惑でネット炎上(画像) | マスコミの気になるニュースをまとめ読み! より
  • Twitterアカウント名が商標権侵害で訴えられそうになった話 に クロネコ より
  • Twitterアカウント名が商標権侵害で訴えられそうになった話 に eamcet*[SEO対策調査自動更新ブログ] | とあるTwitterユーザー、商標権侵害を理由にTwitterアカウント名の変更を求められる より
  • Twitterアカウント名が商標権侵害で訴えられそうになった話 に 心は萌え より

Tweet on Twitter

EvoLve theme by Theme4Press  •  Powered by WordPress anoncom blog