|
发表于 2010-6-12 07:58:48
|
显示全部楼层
我觉得楼主的问题有一直可以解决的办法,我的个人看法是可以通常传递类名的形式,然后在控制器中实例化类
PHP复制代码
function my_method( $object ){
$obj = new $object;
$title = $obj->title;
$subject = $obj->subject ;
}
复制代码
然后还得结合php5的类的自动加载功能,参考一下ci第三方类库Datamapper的实现方式
PHP复制代码
//DataMapper::autoload静态方法首先在APPPATH的model文章就寻找类,如果没有找到就
//在子文件夹中递归搜索
spl_autoload_register('DataMapper::autoload');
static function autoload ($class)
{
// Don't attempt to autoload CI_ or MY_ prefixed classes
if (in_array(substr($class, 0, 3), array('CI_', 'MY_')))
{
return;
}
// Prepare class
$class = strtolower($class);
// Prepare path
$path = APPPATH . 'models';
// Prepare file
$file = $path . '/' . $class . EXT ;
// Check if file exists, require_once if it does
if (file_exists($file))
{
require_once($file);
}
else
{
// Do a recursive search of the path for the class
DataMapper ::recursive_require_once($class, $path);
}
}
// --------------------------------------------------------------------
/**
* Recursive Require Once
*
* Recursively searches the path for the class, require_once if found.
*
* @access public
* @param string
* @param string
* @return void
*/
static function recursive_require_once ($class, $path)
{
if ($handle = opendir($path))
{
while (FALSE !== ($dir = readdir($handle)))
{
// If dir does not contain a dot
if (strpos($dir, '.') === FALSE)
{
// Prepare recursive path
$recursive_path = $path . '/' . $dir;
// Prepare file
$file = $recursive_path . '/' . $class . EXT ;
// Check if file exists, require_once if it does
if (file_exists($file))
{
require_once($file);
break;
}
else if (is_dir($recursive_path))
{
// Do a recursive search of the path for the class
DataMapper ::recursive_require_once($class, $recursive_path);
}
}
}
closedir($handle);
}
}
复制代码 |
|