Zend 框架中的常见常量变量 [英] common constant variables in Zend Framework

查看:27
本文介绍了Zend 框架中的常见常量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建包含所有应用程序常量变量的类的最佳位置在哪里?
是吗 :- 引导程序
- 在应用程序通用库中

Where is the best place to create a class that contain all the application constant variables ?
is it : - Bootstrap
- In the Application Common library

例如:1- 如果此记录没有图像,当我从数据库中检索图像名称时,我想在某处放置一个默认值,以便我可以在我的模型中使用它

for example : 1- When i retrieve an image name from a database if this record doesnt have an image , i want to put a default value somewhere so i can use it in my models

** 我在所有应用程序中使用的常量,因此如果我更改它,我不想回到我的代码中的所有内容并在任何地方更改它

** a constants that i use in all my application so if i change it , i dont want to go back to all in my code and change it everywhere

推荐答案

application.ini 是最好的地方,例如在那里定义一些常量

application.ini is the best place for example define some constant there

constants.PUBLIC_PATH =  APPLICATION_PATH "/../public/"
constants.THEME = blue

然后在你的引导程序中做

Then in your bootstrap do

protected function setConstants($constants)
{
    foreach($constants as $name => $value)
    {
         if(!defined($name))
            define($name, $value);
    }

}

ZF 从配置中获取常量"并在引导程序中调用 setConstants 方法,传递所有以常量为前缀的行,因此它是一个数组.

ZF take 'constants' from config and call setConstants method in your bootstrap passing all lines prefixed by constants hence its an array .

这篇关于Zend 框架中的常见常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆