参数化/配置php应用程序的最简单方法 人性化的数据序列化

Lui*_*uot 5 php customization text

当开发时间很重要时,其他人可以帮助的是一个目标.我的PHP应用程序现在参数化并配置了包含以下形式的数组的包含文件:

$config = array(
   'company'            => 'BMC' ,       // the visible company name
   'aplicable_tax'      => .21   ,       // the IVA tax
   'context_arr'        => array(
        'case1'             =>    12,    // the defalul value
        'case2'             =>    13,
        'case3'             =>    14
                           ),
   'EN_welcome_text'       => 'hello',   // do NOT translate on regionalization

   // xx comparation matrix
   'xx_maxref'=> 5,
   'xx_range' => array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
   'xx_comp'  => array( 
    //  V Other V  > I >>   0, 1, 2, 3, 4, 5, 6, 7, 8, 9
        /*  0 */     array( 0, 3, 5, 5, 5, 5, 5, 5, 5, 5),
        /*  1 */     array(-3, 0, 3, 5, 5, 5, 5, 5, 5, 5),
        /*  2 */     array(-5,-3, 0, 3, 5, 5, 5, 5, 5, 5),
        /*  3 */     array(-5,-5,-3, 0, 3, 5, 5, 5, 5, 5),
        /*  4 */     array(-5,-5,-5,-3, 0, 3, 5, 5, 5, 5),
        /*  5 */     array(-5,-5,-5,-5,-3, 0, 3, 5, 5, 5),
        /*  6 */     array(-5,-5,-5,-5,-5,-3, 0, 3, 5, 5),
        /*  7 */     array(-5,-5,-5,-5,-5,-5,-3, 0, 3, 5),
        /*  8 */     array(-5,-5,-5,-5,-5,-5,-5,-3, 0, 3),
        /*  9 */     array(-5,-5,-5,-5,-5,-5,-5,-5,-3, 0),
),


// and so on
// and so on
// and so on
)
Run Code Online (Sandbox Code Playgroud)

但是这种方法是不安全的,因为任何允许的编辑器都可以注入PHP代码或错误.

我的问题:

  • 您能否建议一种简单灵活的格式,为三方提供参数化PHP应用程序的方法?
  • 是否有从该格式转换为PHP的转换脚本?

isr*_*a17 2

YAML 是 XML 的一个很好的替代方案。YAML 的语法很轻,任何人都易于读/写。另一个要点是 YAML 区分了哈希和数组。我推荐您 symfony 独立组件:http://fabien.potencier.org/article/40/the-state-of-yaml-in-php

你的文件看起来像:

company: BMC         #the visible company name
aplicable_tax: 0.21  #the IVA tax
context_arr:
    case1: 12       #the defalul value
    case2: 13
    case3: 14
EN_welcome_text: hello #do NOT translate on regionalization

#xx comparation matrix
xx_maxref: 5
xx_range: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
xx_comp:
# V Other V  > I >>   0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    -[  0, 3, 5, 5, 5, 5, 5, 5, 5, 5 ] # [ 0 ]
    -[ -3, 0, 3, 5, 5, 5, 5, 5, 5, 5 ] # [ 1 ]
    -[ -5,-3, 0, 3, 5, 5, 5, 5, 5, 5 ] # [ 2 ]
    -[ -5,-5,-3, 0, 3, 5, 5, 5, 5, 5 ] # [ 3 ]
    -[ -5,-5,-5,-3, 0, 3, 5, 5, 5, 5 ] # [ 4 ]
    -[ -5,-5,-5,-5,-3, 0, 3, 5, 5, 5 ] # [ 5 ]
    -[ -5,-5,-5,-5,-5,-3, 0, 3, 5, 5 ] # [ 6 ]
    -[ -5,-5,-5,-5,-5,-5,-3, 0, 3, 5 ] # [ 7 ]
    -[ -5,-5,-5,-5,-5,-5,-5,-3, 0, 3 ] # [ 8 ]
    -[ -5,-5,-5,-5,-5,-5,-5,-5,-3, 0 ] # [ 9 ]
Run Code Online (Sandbox Code Playgroud)

YAML 具有 XML 的所有优势,甚至更多。