开发者

Variables versus constants versus associative arrays in PHP

开发者 https://www.devze.com 2022-12-29 09:26 出处:网络
I\'m working on a small project, and need to implement internationalization support somehow. I am thinking along the lines of using constants to define a lot of symbols for text in one file, which cou

I'm working on a small project, and need to implement internationalization support somehow. I am thinking along the lines of using constants to define a lot of symbols for text in one file, which could be included subsequently. However, I'm not sure if using variables is faster, or if I can get away with using associative ar开发者_如何学运维rays without too much of a performance hit.

What's better for defining constant values in PHP, performance-wise -- constants defined using define("FOO", "..."), or simple variables like $foo = "...", or associative arrays like $symbols["FOO"]?


There's not going to be any noticeable difference in performance so don't worry about that. Do whatever is going to be easier to maintain.

Personally i'd go with an associative array if it's not too complicated. If things are a little more complicated then use gettext.


Think of gettext

This question is not performance related.
No syntax issue (i.e. constants vs. variables) can affect performance


In compiled languages, constants are replaced by the compiler with their constant value affect both performance and memory requirements.

In PHP define is notoriously slow. So much so that somebody even wrote a PHP extension to remedy the problem: http://pecl.php.net/package/hidef


Now that I think of it, this extension could a nice option to consider for you internationalization support.
It allows you to define the constants in a separate ini file, looking something like:

int N     = -191
str ABC   = "xyz"
float PIE = 3.1419
bool ART  = true

which is easy to read/comprehend for non-programmers (translators for example)


You might want to try using Zend_Translate and Zend_Locale plus whatever other components you need (Zend_Date, Zend_Currency, etc..). Its going to beef up the size of your app in terms of filespace, but you can cache all your translations easily and it allows you to choose from a number of different fomrats for your translations (getext, tmx, csv, xliff, etc.).

0

精彩评论

暂无评论...
验证码 换一张
取 消