PHP开发,个人觉得还是要轻量级去做框架,分为3个层次:data 层 modle层 control 层,而我们在data层里面可能会用到设计模式的单一模式,而在modle层里面可能采用简单工厂模式,而在control层里面有order模式,和代理(proxy层,让框架去自动调用),而这里面往往这些都在一个依赖倒转原则下(也就是会修电脑但你不会修收音机),其实就是一个里氏代换原则,让data modle control彻底的分开,就像电脑插槽一样,随时随地可以替换,而且很容易查到问题 的所在。
工厂类调用方式:
$smarty = ClsFactory::Create ( "libs::MySmarty" );
$this->template = 'myrecord.html';
工厂类实现方法:
class ClsFactory
{
function Create($classname)
{
$c = explode("::",$classname);
if(count($c) == 2)
{
$package = $c[0];
$classname = $c[1];
}
if(!class_exists($classname))
{
$classpath = SERVER_ROOT."/libs/";
//if($package) $classpath .= $package."/";
$classfile = $classpath.strtolower($classname).".class.php";
if(!file_exists($classfile)) return 0;
include($classfile);
}
if(!class_exists($classname)) {
return 0;
}
return new $classname;
}
}
路径定义:
./stdafx.php:define ( "SERVER_ROOT", dirname ( __FILE__ ) );
全局变量:
function init_www()
{
$need_include = array (
SERVER_ROOT,
SERVER_ROOT . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR,
SERVER_ROOT . DIRECTORY_SEPARATOR . "program" . DIRECTORY_SEPARATOR,
dirname ( SERVER_ROOT ) . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR );
$include_path = explode ( PATH_SEPARATOR, ini_get ( 'include_path' ) );
foreach ( $need_include as $path )
{
if (! in_array ( $path, $include_path ))
{
$include_path [] = $path;
}
}
ini_set ( 'include_path', join ( PATH_SEPARATOR, $include_path ) );
}
init_www ();
工厂类调用方式:
$smarty = ClsFactory::Create ( "libs::MySmarty" );
$this->template = 'myrecord.html';
工厂类实现方法:
class ClsFactory
{
function Create($classname)
{
$c = explode("::",$classname);
if(count($c) == 2)
{
$package = $c[0];
$classname = $c[1];
}
if(!class_exists($classname))
{
$classpath = SERVER_ROOT."/libs/";
//if($package) $classpath .= $package."/";
$classfile = $classpath.strtolower($classname).".class.php";
if(!file_exists($classfile)) return 0;
include($classfile);
}
if(!class_exists($classname)) {
return 0;
}
return new $classname;
}
}
路径定义:
./stdafx.php:define ( "SERVER_ROOT", dirname ( __FILE__ ) );
全局变量:
function init_www()
{
$need_include = array (
SERVER_ROOT,
SERVER_ROOT . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR,
SERVER_ROOT . DIRECTORY_SEPARATOR . "program" . DIRECTORY_SEPARATOR,
dirname ( SERVER_ROOT ) . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR );
$include_path = explode ( PATH_SEPARATOR, ini_get ( 'include_path' ) );
foreach ( $need_include as $path )
{
if (! in_array ( $path, $include_path ))
{
$include_path [] = $path;
}
}
ini_set ( 'include_path', join ( PATH_SEPARATOR, $include_path ) );
}
init_www ();
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1830/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-8-18 11:50
评论列表