如何在舊的PHP系統(tǒng)中使用PHP 5.3之后的庫(kù)
所謂老的系統(tǒng),是指沒有使用PHP 5.3以上命名空間(namespace)特性編碼的系統(tǒng)。
但是,只要你的系統(tǒng)運(yùn)行在 PHP 5.3及以上的環(huán)境,在你的老系統(tǒng)中,是可以使用這些基于新特性如命名空間編碼的庫(kù)或代碼的。
以前只是有潔癖不用而已。
比如,我是個(gè)工具控,想讓所用的禪道系統(tǒng)也像那些國(guó)際化開源 Issue 項(xiàng)目一樣有一套標(biāo)準(zhǔn)開放的 API - 禪道本身是有套 html、json 自適配模式可以當(dāng)接口用的,可以用于其他客戶端或系統(tǒng)集成。這幾天在嘗試編寫的用于兼容 Redmine REST 風(fēng)格接口的禪道 PMS API,就有意識(shí)的用了這種混合的寫法。
由于要兼容 Redmine 的 REST 風(fēng)格,首先選用了 Slim 這個(gè)微服務(wù)框架,毫無疑問,它是要求運(yùn)行環(huán)境>5.3的,但我總得復(fù)用禪道已有的代碼,這樣效率才高。
原理很簡(jiǎn)單,就是一根反斜杠,或者兩根。
先用composer 初始化了slim 庫(kù)。
重點(diǎn)部位的代碼:
入口文件 index.php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/inc/zentao/nb/Autoloader.php';
\zentao\nb\Autoloader::register();
$app = \zentao\core\Application::app(dirname(ZTNB_ROOT)); //禪道的router
$slim = new \Slim\Slim();
$routes = require __DIR__ . '/data/config/routes.php';
foreach ($routes as $method => $_routes) {
if ($_routes) {
foreach ($_routes as $rule => $map) {
$slim->$method($rule, '\\zentao\\nb\\resource\\' . $map);
}
}
}
$slim->run();
\zentao\core\Application 是獨(dú)立封裝的兼容禪道原來運(yùn)行環(huán)境的類,由禪道中的 framework/router.class.php 改造而來,主要用于加載禪道中的相關(guān)資源如配置文件、模型等。精華應(yīng)該在這里面,主要是加了一些“\”來讓微服務(wù)中能跑起來禪道運(yùn)來的運(yùn)行環(huán)境,并作為一個(gè)命名空間的橋梁可以在新的代碼中調(diào)用。
再看看資源類的父類 \zentao\nb\Resource,片段
<?php
namespace zentao\nb;
/**
* 資源類 父類
*/
class Resource {
public function __construct() {
}
/**
* Load the model file of one module.
*
* @param string $methodName The method name, if empty, use current module's name.
* @access public
* @return object|bool If no model file, return false. Else return the model object.
*/
protected function loadModel($moduleName) {
$modelFile = \helper::setModelFile($moduleName);
/* If no model file, try load config. */
if (!\helper::import($modelFile)) {
$this->app->loadConfig($moduleName, false);
$this->app->loadLang($moduleName);
$this->dao = new dao();
return false;
}
$modelClass = class_exists('ext' . $moduleName . 'model') ? 'ext' . $moduleName . 'model' : $moduleName . 'model';
$modelClass = '\\' . $modelClass;
if (!class_exists($modelClass))
$this->app->triggerError(" The model $modelClass not found", __FILE__, __LINE__, $exit = true);
$this->$moduleName = new $modelClass();
$this->dao = $this->$moduleName->dao;
return $this->$moduleName;
}
這樣可以在資源類中調(diào)用禪道的 model 類。
還有另外一種用法,加載語言包:
<?php
namespace zentao\nb\resource;
use zentao\nb\enum\BugType;
/**
* 項(xiàng)目自行定義的問題分類
*/
class IssueCategory extends \zentao\nb\resource {
public function fetchAll($format = 'json') {
global $app;
$types = $app->loadLang('bug')->bug->typeList;
$issue_categories = array();
foreach ($types as $key => $name) {
$issue_categories[] = array('id' => BugType::getIdByInterId($key), 'name' => $name);
}
echo json_encode(array('issue_categories' => $issue_categories));
}
/**
* 根據(jù)項(xiàng)目來取其中定義的分類
* @param int $projectId
* @param string $format
*/
public function fetchAllByProjectId($projectId, $format = 'json') {
$model = $this->loadModel('project');
$project = $model->getById($projectId);//TODO 支持按項(xiàng)目代號(hào)查找
if (!$project) {
$this->responseNotExixted();
}
global $app;
$types = $app->loadLang('bug')->bug->typeList;
$issue_categories = array();
foreach ($types as $key => $name) {
$issue_categories[] = array('id' => BugType::getIdByInterId($key), 'project' => array('id' => $projectId, 'name' => $project->name), 'name' => $name);
}
echo json_encode(array(
'issue_categories' => $issue_categories,
'total_count' => 2
));
}
}
基本項(xiàng)目結(jié)構(gòu)如下:

項(xiàng)目只是初步成型,尚未完成。

這是在 NB 中的任務(wù)列表。

這是在 NB 中的任務(wù)詳情。
以上就是告訴大家如何在舊的PHP系統(tǒng)中使用PHP 5.3之后的庫(kù),希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)的字符串匹配算法示例【sunday算法】
這篇文章主要介紹了PHP實(shí)現(xiàn)的字符串匹配算法,簡(jiǎn)單描述了sunday算法的概念與原理,并結(jié)合實(shí)例形式分析了php基于sunday算法實(shí)現(xiàn)字符串匹配操作相關(guān)技巧,需要的朋友可以參考下2017-12-12
PHP實(shí)現(xiàn)JWT的Token登錄認(rèn)證
這篇文章通過實(shí)例代碼介紹了PHP實(shí)現(xiàn)JWT的Token登錄認(rèn)證的方式,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
Fatal error: Allowed memory size of 134217728 bytes exhauste
這篇文章主要介紹了Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)錯(cuò)誤的解決方法,需要的朋友可以參考下2014-11-11
php實(shí)現(xiàn)四舍五入的方法小結(jié)
這篇文章主要介紹了php實(shí)現(xiàn)四舍五入的方法,實(shí)例總結(jié)了php實(shí)現(xiàn)四舍五入的三種常用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過濾方法
這篇文章主要介紹了PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過濾方法,可實(shí)現(xiàn)針對(duì)外部鏈接增加rel="nofollow"的功能,有助于網(wǎng)站SEO建設(shè)以及避免不必要的損失,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
PHP單例模式Singleton Pattern的原理與實(shí)現(xiàn)介紹
單例就是單實(shí)例的意思,即在系統(tǒng)全局,一個(gè)類只創(chuàng)建一個(gè)對(duì)象,并且在系統(tǒng)全局都可以訪問這個(gè)對(duì)象而不用重新創(chuàng)建。本文將通過示例為大家詳細(xì)講解Java單例模式的使用,需要的可以參考一下2023-03-03
php實(shí)現(xiàn)獲取農(nóng)歷(陰歷)、節(jié)日、節(jié)氣的類與用法示例
這篇文章主要介紹了php實(shí)現(xiàn)獲取農(nóng)歷(陰歷)、節(jié)日、節(jié)氣的類與用法,結(jié)合實(shí)例形式分析了php日期工具類Lunar的具體定義與獲取農(nóng)歷日期、節(jié)氣等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
php模塊memcache和memcached區(qū)別分析
談及php搭配memcached使用,已經(jīng)是老生常談的問題。但是有一些細(xì)節(jié),不見得人人清楚。比如說php的模塊memcache和memcached有什么區(qū)別等。下面我就簡(jiǎn)單介紹一下。2011-06-06

