Home  >  Article  >  Backend Development  >  php 分离页面配置与代码逻辑的方法

php 分离页面配置与代码逻辑的方法

WBOY
WBOYOriginal
2016-07-25 08:56:251065browse
本文介绍下,php实现分离页面配置与代码逻辑的具体方法,有需要的朋友,参考下吧。

在php编程中,为了代码清晰结构性强,通常需要分离页面配置参数和代码逻辑。约定唯一的命名空间,实现基本的所谓结构和行为分离。

第一种方式,提供一个暴露在 window 的全局对象,这里是 TB 变量。然后所有的代码被封装在该全局对象下。

<?php
// 创建命名空间
var TB = {};

/**
 * 定义初始化入口函数
 * @method init
 * @param {JSON} config 页面配置参数
 */
TB.init = function(config){
 console.log(config.demo);
}

// 页面配置参数的初始化
TB.init({
    demo: 'http://bbs.it-home.org'
});
或者,匿名函数模式,把参数传给匿名函数,并局限在该函数体内部。隔离作用域链,所谓闭包。

/**
 * 定义匿名函数
 * @param {JSON} config 页面配置参数
 */
(function(config){

 console.log(config.demo);

})({
 demo : 'http://bbs.it-home.org'
});
//第一种可能更适合于组织复杂代码,模块化,颗粒化。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn