CONSTANTS and PHP Class Definitions
Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, or string (no array or other object types) -- right away.
<?php
define('MIN_VALUE', '0.0'); // RIGHT - Works OUTSIDE of a class definition.
define('MAX_VALUE', '1.0'); // RIGHT - Works OUTSIDE of a class definition.
//const MIN_VALUE = 0.0; WRONG - Works INSIDE of a class definition.
//const MAX_VALUE = 1.0; WRONG - Works INSIDE of a class definition.
class Constants
{
//define('MIN_VALUE', '0.0'); WRONG - Works OUTSIDE of a class definition.
//define('MAX_VALUE', '1.0'); WRONG - Works OUTSIDE of a class definition.
const MIN_VALUE = 0.0; // RIGHT - Works INSIDE of a class definition.
const MAX_VALUE = 1.0; // RIGHT - Works INSIDE of a class definition.
public static function getMinValue()
{
return self::MIN_VALUE;
}
public static function getMaxValue()
{
return self::MAX_VALUE;
}
}
?>
#Example 1:
You can access these constants DIRECTLY like so:
* type the class name exactly.
* type two (2) colons.
* type the const name exactly.
#Example 2:
Because our class definition provides two (2) static functions, you can also access them like so:
* type the class name exactly.
* type two (2) colons.
* type the function name exactly (with the parentheses).
<?php
#Example 1:
$min = Constants::MIN_VALUE;
$max = Constants::MAX_VALUE;
#Example 2:
$min = Constants::getMinValue();
$max = Constants::getMaxValue();
?>
Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).
第 13章定数
定数は簡単な値のためのID(名前)です。この名前が示すように、定数の値は スクリプト実行中に変更できません (magic constantsは 例外で、これらは実際は定数ではありません)。 デフォルトで定数では大文字小文字を区別します。慣習的に、 定数は常に大文字で表記されます。
定数の名前は、PHP のラベルと同じ規則に従います。有効な定数の名前は、 文字またはアンダースコアで始まり、任意の数の文字、数字、 アンダースコアが後に続きます。正規表現で示すと次のようになります。 [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
ティップ: 付録R もご覧になるとよいでしょう。
注意: 本節の目的においては、文字は a-z, A-Z, および127から255まで (0x7f-0xff)のASCII文字を指します。
superglobalsと同様に定数のスコープはグローバルです。 つまり、スコープによらずスクリプトの中ではどこでも定数に アクセスできます。スコープの詳細についてはマニュアルの 変数のスコープ をご覧ください。
構文
define() 関数を使用することにより、 定数を定義することが可能です。定数が一度定義されると、 変更または未定義とすることはできません。
定数に指定できるのは、スカラデータ (boolean, integer, double, string) のみです。 resource の定数を指定しないでください。
単に定数の名前を指定することにより、その値を得ることが可能です。 変数とは異なり、その前に $ は不要です。 定数の名前を動的に得る必要がある場合、定数の値を読むために関数 constant() を使用することも可能です。 定義済の定数の一覧を得るには、 get_defined_constants() を使用してください。
注意: 定数と(グローバル)変数は、異なる名前空間にあります。 例えば、TRUE と $TRUE は違うものを意味します。
未定義の定数を使用した場合、ちょうどstringとして コールしたかのように(CONSTANT vs "CONSTANT")、 PHPはその定数自体の名前を使用したと仮定します。 この際、E_NOTICE が発生します。 ある定数が設定されているかどうかを知るには、 defined() 関数を使用してください。 なぜ $foo[bar]が間違っている (まずbarを定数としてdefine()しなければ) のかというマニュアルもご覧ください。 定数がセットされているかを単にチェックするには defined()を使用してください。
変数との違いは次のようになります。
定数は、前にドル記号($)を要しません。
定数を定義することができるのは、define()関数 のみです。単なる代入による定義はできません。
定数は、定義することができ、変数のスコープ規則に関係なく、あら ゆる場所からアクセス可能です。
定数は一度設定されると再定義または未定義とすることはできません。
定数は、スカラー値としてのみ評価可能です。
オブジェクト定数 も参照ください。
PHP will allow characters other than those shown for both variable names and constants, and therefore probably functin names, too. And I'm pretty sure array indexes also.
Many others are allowed, while some are not. It seems like a craps-shoot at first, but there is a 'bit' of reason...
you can do this:
<?php
$x´; // using the "acute (reverse) accent mark" (#182)
?>
but not:
<?php
$x′; // using the "prime mark" (#8242)
?>
but you can do:
<?php
$x‡†±√2×π // using double dagger, dagger, plus-minus, square-root, the number 2, the "times symbol" and the greek letter pi (lowercase).
?>
but not:
<?php
$x♂♀◊∆; // using male, female, lozenge, mathematical increment symbol.
?>
You can do this:
<?php
define('≈PI', M_PI); // 180 degrees = π radians ≈ 3.141592654 radians
define('≈180°', M_PI);
define('≈PI÷2', M_PI_2); // 90 degrees ≈ 1.570796327 radians
define('≈90°', M_PI_2);
define('≈PI÷4', M_PI_4); // 45 degrees ≈ 0.785398163 radians
define('≈45°', M_PI_4);
define('≈PI×3÷2', ≈PI+≈PI÷2); // 270 degrees ≈ 4.71238898 radians
define('≈270°', ≈PI×3÷2);
define('≈PI×2', ≈PI*2); // 360 degrees = 2π radians ≈ 6.283185307 radians
define('≈360°', ≈PI×2);
?>
So essentially, you must check each character for acceptance by PHP if you want to use them, but they can really add a semantical value in some cases, thus making your code easier to read and understand..
A nice way to set and verify a constant is not already set:
defined('CONSTANT') or define('CONSTANT', 'value');
If the constant is defined the expression resolves to false, if the constant isn't set it will be defined.
I recently found I needed a way of retrieving the value of a constant dynamically - e.g. trying to find the value of FOO_BAR by passing 'FOO_' . $someVariableWithValueBAR. I came up with the following solution:
<?php
define('FOO_BAR','It works!');
define('FOO_FOO_BAR','It works again!');
// prints 'It works!'
$changing_variable = 'bar';
echo constant('FOO_' . strtoupper($changing_variable));
// prints 'It works again!'
$changing_variable = 'foo_bar';
echo constant('FOO_' . strtoupper($changing_variable));
?>
Note the use of strtoupper() as constants should be defined in uppercase for good practice - feel free to remove if you have constants defined in lowercase or you can set $changing_variable as uppercase.
Might be of some use to someone!
Note that constant name must always be quoted when defined.
e.g.
define('MY_CONST','blah') - correct
define(MY_CONST,'blah') - incorrect
The following error message also indicates this fact:
Notice: Use of undefined constant MY_CONST - assumed 'MY_CONST' in included_script.php on line 5
Note the error message gives you some incorrect information. 'MY_CONST' (with quotes) doesn't actually exist anywhere in your code. The error _is_ that you didn't quote the constant when you defined it in the 'assumed' file.
If you are looking for predefined constants like
* PHP_OS (to show the operating system, PHP was compiled for; php_uname('s') might be more suitable),
* DIRECTORY_SEPARATOR ("\\" on Win, '/' Linux,...)
* PATH_SEPARATOR (';' on Win, ':' on Linux,...)
they are buried in 'Predefined Constants' under 'List of Reserved Words' in the appendix:
http://www.php.net/manual/en/reserved.constants.php
while the latter two are also mentioned in 'Directory Functions'
http://www.php.net/manual/en/ref.dir.php
Note that constants can also be used as default argument values
so the following code:
<?php
define('TEST_CONSTANT','Works!');
function testThis($var=TEST_CONSTANT) {
echo "Passing constants as default values $var";
}
testThis();
?>
will produce :
Passing constants as default values Works!
(I tried this in both PHP 4 and 5)
1) Constants are invaluable when you want to be sure that *nobody* changes your important piece of data through lifetime of script -- especially when you're developing in team -- as this can cause strange, hard to track bugs.
2) Using constants is prefered over ``magic values'', as it leads to self-documenting code. Also saves you from scanning and tweaking tens of files should the value ever change.
Consider example: <?php
if ( $headers['code'] = 505 ) { //wth is 505? What do following code do? ?>
versus: <?php
if ( $headers['code'] = HTTP_VERSION_NOT_SUPPORTED ) {
$this->useHttp = '1.0'; ?>
In response to ``kencomer'':
3) Why not to use <?php
define( 'DEBUG', FALSE );
define( 'DEBUG', TRUE ); ?>
and comment one of them out as needed when developing/deploying?
That'd save a lot of ugly ``if ( defined( 'DEBUG' ) && DEBUG ) {}''.
4) For debugging toggled on/off you pretty often want to use assert() anyway. You're free to turn it on/off at any moment (thou you better do it only once ;) ). assert() gives some nice details upon failed assertion, like file/line/function and context (that's invaluable!)
I find variables much more flexible than constants because variables can be used inside quotes and heredocs etc. Especially for language systems, this is nice.
As stated in one of the previous notes, there is no speed penalty by using variables. However, one issue is that you risc name collision with existing variables. When implementing a language system I simply found that adding a prefix to all the variables was the way to go, for example:
$LNG_myvar1 = "my value";
That is easier and performs faster than using arrays like
$LNG['myvar'] = "my value";
As a final note, implementing a new superglobal in PHP would make using constants much more beneficial. Then it could be used in qoutes like this:
"The constant myconst has the value $CONSTANTS[myconst] !"
It is possible to define constants that have the same name as a built-in PHP keyword, although subsequent attempts to actually use these constants will cause a parse error. For example in PHP 5.1.1, this code
<?php
define("PUBLIC", "Hello, world!");
echo PUBLIC;
?>
gives the error
Parse error: syntax error, unexpected T_PUBLIC in test.php on line 3
This is a problem to be aware of when converting PHP4 applications to PHP5, since that release introduced several new keywords that used to be legal names for constants.
Being a belt and suspenders person, when I use a constant to do flow control (i.e., using constants to determine which version of a section of the program should be used), I always use something like:
if ( defined('DEBUG') && TRUE===DEBUG )
If you accidentally use DEBUG somewhere before it is defined, PHP will create a new constant called DEBUG with the value 'DEBUG'. Adding the second comparison will prevent the expression from being TRUE when you did not intentionally create the constant. For the constant DEBUG, this would rarely be a problem, but if you had (e.g.) a constant used to determine whether a function was created using case-sensitive comparisons, an accidental creation of the constant IGNORE_CASE having the value 'IGNORE_CASE' could drive you up the wall trying to find out what went wrong, particularly if you had warnings turned off.
In almost all code I write, I put this function definition in my configuration section:
<?php
if (!function_exists("debug_print")) {
if ( defined('DEBUG') && TRUE===DEBUG ) {
function debug_print($string,$flag=NULL) {
/* if second argument is absent or TRUE, print */
if ( !(FALSE===$flag) )
print 'DEBUG: '.$string . "\n";
}
} else {
function debug_print($string,$flag=NULL) {
}
}
}
?>
Then, in my code, I'll sprinkle liberal doses of debug code like :
<?php
define("DEBUG_TRACK_EXAMPLE_CREATION",FALSE);
class Example extends Something {
__construct($whatever) {
debug_print( "new instance of Example created with '$whatever'\n",DEBUG_TRACK_EXAMPLE_CREATION);
}
}
?>
and :
<?php
debug_print("finished init.\n");
?>
In the first case, I would not want to see that message every time I went into DEBUG mode, so I made it a special case. The second case is always printed in DEBUG mode. If I decide to turn everything on, special cases and all, all I have to do is comment out the "if" line in debug_print() and presto magicko! It costs a little and gains a lot.
As another belt-and-suspenders aside, notice that, unlike most people, I put the language constant (e.g.,TRUE, "string", etc.) on the left side of the comparison. By doing that, you can never accidentally do something like
if ( $hard_to_find_error="here" )
because you always write it as
if ( "here"==$no_error )
or, if you got it wrong,
if ( "here"=$easy_to_find_parse_error )
It is so easy to create a constant that the php novice might do so accidently while attempting to call a function with no arguments. For example:
<?php
function LogoutUser(){
// destroy the session, the cookie, and the session ID
blah blah blah;
return true;
}
function SessionCheck(){
blah blah blah;
// check for session timeout
...
if ($timeout) LogoutUser; // should be LogoutUser();
}
?>
OOPS! I don't notice my typo, the SessionCheck function
doesn't work, and it takes me all afternoon to figure out why not!
<?php
LogoutUser;
print "new constant LogoutUser is " . LogoutUser;
?>
PHP Modules also define constants. Make sure to avoid constant name collisions. There are two ways to do this that I can think of.
First: in your code make sure that the constant name is not already used. ex. <?php if (! defined("CONSTANT_NAME")) { Define("CONSTANT_NAME","Some Value"); } ?> This can get messy when you start thinking about collision handling, and the implications of this.
Second: Use some off prepend to all your constant names without exception ex. <?php Define("SITE_CONSTANT_NAME","Some Value"); ?>
Perhaps the developers or documentation maintainers could recommend a good prepend and ask module writers to avoid that prepend in modules.
An undefined constant evaluates as true when not used correctly. Say for example you had something like this:
settings.php
<?php
// Debug mode
define('DEBUG',false);
?>
test.php
<?php
include('settings.php');
if (DEBUG) {
// echo some sensitive data.
}
?>
If for some reason settings.php doesn't get included and the DEBUG constant is not set, PHP will STILL print the sensitive data. The solution is to evaluate it. Like so:
settings.php
<?php
// Debug mode
define('DEBUG',0);
?>
test.php
<?php
include('settings.php');
if (DEBUG == 1) {
// echo some sensitive data.
}
?>
Now it works correctly.
before embarking on creating a language system I wanted to see if there was any speed advantage to defining language strings as constants vs. variables or array items. It is more logical to define language strings as constants but you have more flexibility using variables or arrays in your code (i.e. they can be accessed directly, concatenated, used in quotes, used in heredocs whereas constants can only be accessed directly or concatenated).
Results of the test:
declaring as $Variable is fastest
declaring with define() is second fastest
declaring as $Array['Item'] is slowest
=======================================
the test was done using PHP 4.3.2, Apache 1.3.27, and the ab (apache bench) tool.
100 requests (1 concurrent) were sent to one php file that includes 15 php files each containing 100 unique declarations of a language string.
Example of each declaration ("Variable" numbered 1 - 1500):
<?php
$GLOBALS['Variable1'] = "A whole lot of text for this variable as if it were a language string containing a whole lot of text";
?>
<?php
define('Variable1' , "A whole lot of text for this variable as if it were a language string containing a whole lot of text");
?>
<?php
$GLOBALS['CP_Lang']['Variable1'] = "A whole lot of text for this variable as if it were a language string containing a whole lot of text";
?>
Here are the exact averages of each ab run of 100 requests (averages based on 6 runs):
variable (24.956 secs)
constant (25.426 secs)
array (28.141)
(not huge differences but good to know that using variables won't take a huge performance hit)
I find using the concatenation operator helps disambiguate value assignments with constants. For example, setting constants in a global configuration file:
<?php
define('LOCATOR', "/locator");
define('CLASSES', LOCATOR."/code/classes");
define('FUNCTIONS', LOCATOR."/code/functions");
define('USERDIR', LOCATOR."/user");
?>
Later, I can use the same convention when invoking a constant's value for static constructs such as require() calls:
<?php
require_once(FUNCTIONS."/database.fnc");
require_once(FUNCTIONS."/randchar.fnc");
?>
as well as dynamic constructs, typical of value assignment to variables:
<?php
$userid = randchar(8,'anc','u');
$usermap = USERDIR."/".$userid.".png";
?>
The above convention works for me, and helps produce self-documenting code.
-- Erich
Warning, constants used within the heredoc syntax (http://www.php.net/manual/en/language.types.string.php) are not interpreted!
Editor's Note: This is true. PHP has no way of recognizing the constant from any other string of characters within the heredoc block.
To get a full path (the equivalent of something like "__PATH__") use
dirname($SCRIPT_FILENAME)
to get the directory name of the called script and
dirname(__FILE__)
to get the directory name of the include file.
