It doesn't introduce much overhead if you make use of conditional function definitions:
<?php
if (function_exists('apc_load_constants')) {
function define_array($key, $arr, $case_sensitive = true)
{
if (!apc_load_constants($key, $case_sensitive)) {
apc_define_constants($key, $arr, $case_sensitive);
}
}
} else {
function define_array($key, $arr, $case_sensitive = true)
{
foreach ($arr as $name => $value)
define($name, $value, $case_sensitive);
}
}
//in your code you just write something like this:
define_array('NUMBERS', Array('ONE' => 1, 'TWO' => 2, 'THREE' => 3));
?>
apc_define_constants
(PECL apc >= 3.0.0)
apc_define_constants — مجموعهای از ثوابت برای بازیابی و تعریف گسترده فراهم میکند.
Description
define() به شدت کند است. چون که فایده اصلی APC برای افزایش کارایی اسکریپتها/برنامهها است، این ساز و کار فراهم شده است تا فرایند تعریف گسترده ثابتها پربازدهتر شود. به هر حال این تابع به خوبی عمل نمینماید.
برای یک راه حل بهتر ضمیمه » hidef از PECL را امتحان کنید.
Note: برای حذف یک مجموعه ثوابت ذخیره شده (بدون پاک کردن کامل کاشه)، یک آرایه خالی به عنوان پارامتر constants ارسال خواهد شد و به صورت موثری مقادیر ذخیره شده را پاک میکند.
Parameters
- key
-
key به عنوان نام مجموعه ثابت ذخیره شده عمل مینماید. این key برای بازیابی ثابتهای ذخیره شده در apc_load_constants() استفاده خواهد شد.
- constants
-
An associative array of constant_name => value pairs. The constant_name must follow the normal constant naming rules. value must evaluate to a scalar value.
- case_sensitive
-
رفتار پیشفرض ثابتها به صورت حساس به بزرگی و کوچکی باید تعیین شود، به عنوان مثال CONSTANT و Constant نشاندهنده دو مقدار متفاوت هستند. اگر این پارامتر برابر با FALSE ارزیابی گردد ثابتها به صورت نشانهای غیرحساس به بزرگی و کوچکی اعلام خواهند شد.
Return Values
Returns TRUE on success or FALSE on failure.
Examples
Example #1 مثال apc_define_constants()
<?php
$constants = array(
'ONE' => 1,
'TWO' => 2,
'THREE' => 3,
);
apc_define_constants('numbers', $constants);
echo ONE, TWO, THREE;
?>
The above example will output:
123
See Also
- apc_load_constants() - بارگذاری یک مجموعه ثابت از کاشه
- define() - Defines a named constant
- constant() - Returns the value of a constant
- Or مرجع ثابتهای PHP
An observation that I've made is that the nature of apc_define_constants() binding the list of constants to a key and then requiring that key to load the constants is limiting. Furthermore, there's no way to append additional constants to a given key.
A solution that I've been adopting is to build a list of constants to be defined, and then do one of two things:
1) if APC is enabled, then use apc_define_constants();
2) ...else loop through the list and define each constant normally.
The problem I've run into is when this process happens at different places in a large application, it can introduce overhead that otherwise wouldn't be there if it was possible to append to an existing list of defined constants in APC.
