How to use the library _php technique after PHP 5.3 in the old PHP system

Source: Internet
Author: User
Tags autoloader redmine

An old system is a system that does not encode using the PHP 5.3 namespace (namespace) attribute.

However, as long as your system is running in PHP 5.3 and above the environment, in your old system, you can use these new features such as namespace-coded libraries or code.

I used to be a neat freak.

For example, I am a tool-control, want to use the Zen system also like those internationalized Open source Issue project as a set of standard open API-chan Tao itself is an HTML, JSON adaptive mode can be used as an interface, can be used for other clients or system integration. These days in an attempt to write a Zen-style PMS API for compatibility with redmine restful interfaces, it is consciously used in this mixed notation.

Because to be compatible with Redmine REST style, first selected Slim this micro-service framework, there is no doubt that it is required to run the environment >5.3, but I have to reuse the Chan Tao already have the code, so efficiency is high.

The principle is very simple, is a backslash, or two root.

First initialize the Slim library with composer.

Key parts of the code:

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)); The router $slim of chan tao

= new \slim\slim ();

$routes = Require __dir__. '/data/config/routes.php ';

foreach ($routes as $method => $_routes) {
 if ($_routes) {
  foreach ($_routes as $rule => $map) {
   $slim-& gt; $method ($rule, ' \\zentao\\nb\\resource\\ '. $map);
  }

}} $slim->run ();

\zentao\core\application is an independent package of compatible Chan Tao original operating environment classes, from the Zen Road in the framework/router.class.php transformation, mainly used to load the relevant resources in Zen channel such as configuration files, Models and so on. The essence should be in here, the main is to add some "\" to allow the micro-service to run up the Zen path of the running environment, and as a bridge of namespaces can be called in the new code.

Then look at the parent class \zentao\nb\resource of the resource class, fragment

<?php namespace Zentao\nb;
  /** * Resource class Parent/class Resource {public Function __construct () {}/** * Load the model file of one module.
  * * @param string $methodName The method name, if empty, with current module ' s name. * @access public * @return Object|bool If No model file and 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; 

 }

This allows you to invoke the Model class in the Zen path in the resource class.

Another use is to load a language pack:

<?php namespace Zentao\nb\resource;

Use Zentao\nb\enum\bugtype;
  /** * Project self-defined problem classification */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);
 The Echo json_encode (Array (' issue_categories ' => $issue _categories)); /** * According to the project to take the definition of the category * @param int $projectId * @param string $format/Public function Fetchallbyprojectid ($
  ProjectID, $format = ' json ') {$model = $this->loadmodel (' project ');
  $project = $model->getbyid ($projectId);//todo support to find if (! $project) {$this->responsenotexixted () by Project ID;
  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 initially formed and has not yet been completed.

This is the list of tasks in NB.

This is the details of the task in NB.

The above is to tell you how to use the old PHP system after PHP 5.3 Library, I hope to help you learn.

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.