How to use libraries after PHP5.3 in the old PHP System

Source: Internet
Author: User
Tags autoloader redmine
This article mainly introduces how to use the library after PHP5.3 in the old PHP System. if you need a friend, refer to the so-called old system, this is a system that does not use the namespace feature encoding of PHP 5.3 or above.

However, as long as your system runs in PHP 5.3 or above environments, you can use these libraries or codes based on new features such as namespace encoding in your old system.

In the past, it was just a matter of cleanliness.

For example, I am a tool controller, like the international open-source Issue projects, the system wants to use a set of standard and open APIs. Zen itself has a set of html and json self-adaption modes that can be used as interfaces, it can be used for integration with other clients or systems. In the past few days, I have tried to write the Zen pms api that is compatible with the Redmine REST interface, and I have consciously used this hybrid method.

To be compatible with the REST style of Redmine, I first chose the microservice framework Slim, which requires the runtime environment to be greater than 5.3, but I have to reuse the existing code of Zen road, this is highly efficient.

The principle is simple, that is, a backslash or two.

First initialize the slim library with composer.

Code for key parts:

Entry file 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 (); $ 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 \ ApplicationIt is an independently encapsulated class compatible with the original running environment of Zen Road. it is transformed from the framework/router. class. php in Zen Road and is mainly used to load related resources such as configuration files and models in Zen road. The essence should be here, mainly adding some "\" to allow the microservice to run in the running environment of the Zen road, and can be called in the new code as a namespace Bridge.

Let's take a look at the parent class \ zentao \ nb \ Resource and fragment of the Resource class.

<? Phpnamespace zentao \ nb;/*** Resource class parent class */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 ($ mod UleName);/* 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 ('text '. $ moduleName. 'model ')? 'Text'. $ 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 ;}

In this way, the model class of zentao can be called in the resource class.

You can also load the language pack:

<? Phpnamespace zentao \ nb \ resource; use zentao \ nb \ enum \ BugType; /*** project-defined problem category */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_encod E (array ('issue _ categories '=> $ issue_categories ));} /*** obtain the category defined by the Project * @ param int $ projectId * @ param string $ format */public function fetchAllByProjectId ($ projectId, $ format = 'json ') {$ model = $ this-> loadModel ('Project'); $ project = $ model-> getById ($ projectId); // TODO supports searching 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 ));}}

The basic project structure is as follows:

The project is only preliminary and has not been completed yet.

This is the task list in NB.

This is the task details in NB.

The above shows you how to use the PHP 5.3 Library in the old PHP System, hoping to help you learn it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.