用了一段时候的ZF,发现zf是一个非常好用的php编程框架
现在把ZF中个人觉得十分不错的模块简单介绍一下.希望对初学者有所帮助
1.Zend_Cache
cache十分灵活,提供了多种前端的cache方法,以及多种后台的数据保存手段,本人十分喜欢这个模块.前端可缓存:Output,File,Function,class,都是派生自Zend_Cache_Core.后台缓存方式可以是File, Sqlite, Memcache…
简单的调用方式:
7200, // cache lifetime of 2 hours
‘automaticSerialization’ => true);
$backendOptions = array( ‘cacheDir’ => ‘./tmp/’ // Directory where to put the cache files
);
// 取得一个Zend_Cache_Core 对象
$cache = Zend_Cache::factory(’Core’, ‘File’, $frontendOptions, $backendOptions);
if(!$result = $cache->load(’myresult’)) { // 缓存不命中;连接到数据库
$db = Zend_Db::factory( [...] );
$result = $db->fetchAll(’SELECT * FROM huge_table’);
$cache->save($result, ‘myresult’); } else {
// cache hit! shout so that we know
echo “This one is from cache!\n\n” }
print_r($result);
?>
2.Zend_Config
大家都知道建立大型网站时少不了写配置文件,方便灵活的控制各种参数,但是随着网站的不断增大,配置文件越来越不好管理.zf提供了这个类确实让程序员方便不少.
配置文件的格式:ini文件
; Production site configuration data[production]
webhost = www.example.com
database.type = pdo_mysql
database.host = db.example.com
database.username = dbuser
database.password = secret
database.name = dbname
[staging : production]
database.host = dev.example.com
database.username = devuser
database.password = devsecret
用Zend_Config_Ini解析
database->host; // prints “dev.example.com”
echo $config->database->name; // prints “dbname”
?>
也可以是XML文件的格式
www.example.com
pdo_mysql
db.example.com
dbuser
secret
dbname
dev.example.com
devuser
devsecret
用Zend_Config_Xml解析
database->host; // prints “dev.example.com”
echo $config->database->name; // prints “dbname”
?>
3.Zend_Debug
静态方法Zend_Debug::dump()打印和返回某个表达式或变量的信息。它是将var_dump()封装而成的。它默认带上了(X)HTML的pre标签,使输出更美观。这是个非常常用的调试工具。
$var参数指定了要输出的表达式或变量。
$label标签参数是用来加在输出信息之前的一段文本。(这非常有用,例如你一次要查看多个变量的信息,你可以为不同变量设置不同label,如“user”,“password”等等,这样不会弄乱–Haohappy注)
dump()总会返回信息,但不一定会输出,这取决于$echo参数,指定是否要将信息输出到屏幕。如果$echo参数指定为true,则会输出返回的结果。无论是否指定$echo参数的值,该方法的返回值都包含表达式或变量的信息。
Zend_Debug::dump()是对PHP函数var_dump()的封装,对输出结果使用 htmlspecialchars()转义,并加上了(X)HTML
标签,更加美观。如果要长期的进行Debug还是用Zend_Log比较好点.

0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.