note 100142 modified in language.oop5.constants by danbrown
| From: | danbrown@php.net | Date: | Tue, 28 Sep 2010 01:45:49 +0000 |
| Subject: | note 100142 modified in language.oop5.constants by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172758@lists.php.net to get a copy of this message | ||
Most people miss the point in declaring constants and confuse then things by trying to declare
things like functions or arrays as constants. What happens next is to try things that are more
complicated then necessary and sometimes lead to bad coding practices. Let me explain...
A constant is a name for a value (but it's NOT a variable), that usually will be replaced in
the code while it gets COMPILED and NOT at runtime.
So returned values from functions can't be used, because they will return a value only at
runtime.
Arrays can't be used, because they are data structures that exist at runtime.
One main purpose of declaring a constant is usually using a value in your code, that you can replace
easily in one place without looking for all the occurences. Another is, to avoid mistakes.
Think about some examples written by some before me:
1. const MY_ARR = "return array(\"A\", \"B\", \"C\",
\"D\");";
It was said, this would declare an array that can be used with eval. WRONG! This is just a string as
constant, NOT an array. Does it make sense if it would be possible to declare an array as constant?
Probably not. Instead declare the values of the array as constants and make an array variable.
2. const magic_quotes = (bool)get_magic_quotes_gpc();
This can't work, of course. And it doesn't make sense either. The function already returns
the value, there is no purpose in declaring a constant for the same thing.
3. Someone spoke about "dynamic" assignments to constants. What? There are no dynamic
assignments to constants, runtime assignments work _only_ with variables. Let's take the
proposed example:
<?php
/**
* Constants that deal only with the database
*/
class DbConstant extends aClassConstant {
protected $host = 'localhost';
protected $user = 'user';
protected $password = 'pass';
protected $database = 'db';
protected $time;
function __construct() {
$this->time = time() + 1; // dynamic assignment
}
}
?>
Those aren't constants, those are properties of the class. Something like "this->time =
time()" would even totally defy the purpose of a constant. Constants are supposed to be just
that, constant values, on every execution. They are not supposed to change every time a script runs
or a class is instantiated.
Conclusion: Don't try to reinvent constants as variables. If constants don't work, just
use variables. Then you don't need to reinvent methods to achieve things for what is already
there.
--was--
Most people miss the point in declaring constants and confuse then things by trying to declare
things like functions or arrays as constants. What happens next is to try things that are more
complicated then necessary and sometimes lead to bad coding practices. Let me explain...
A constant is a name for a value (but it's NOT a variable), that usually will be replaced in
the code while it gets COMPILED and NOT at runtime.
So returned values from functions can't be used, because they will return a value only at
runtime.
Arrays can't be used, because they are data structures that exist at runtime.
One main purpose of declaring a constant is usually using a value in your code, that you can replace
easily in one place without looking for all the occurences. Another is, to avoid mistakes.
Think about some examples written by some before me:
1. const MY_ARR = "return array(\"A\", \"B\", \"C\",
\"D\");";
It was said, this would declare an array that can be used with eval. WRONG! This is just a string as
constant, NOT an array. Does it make sense if it would be possible to declare an array as constant?
Probably not. Instead declare the values of the array as constants and make an array variable.
2. const magic_quotes = (bool)get_magic_quotes_gpc();
This can't work, of course. And it doesn't make sense either. The function already returns
the value, there is no purpose in declaring a constant for the same thing.
3. Someone spoke about "dynamic" assignments to constants. What? There are no dynamic
assignments to constants, runtime assignments work _only_ with variables. Let's take the
proposed example:
/**
* Constants that deal only with the database
*/
class DbConstant extends aClassConstant {
protected $host = 'localhost';
protected $user = 'user';
protected $password = 'pass';
protected $database = 'db';
protected $time;
function __construct() {
$this->time = time() + 1; // dynamic assignment
}
}
Those aren't constants, those are properties of the class. Something like "this->time =
time()" would even totally defy the purpose of a constant. Constants are supposed to be just
that, constant values, on every execution. They are not supposed to change every time a script runs
or a class is instantiated.
Conclusion: Don't try to reinvent constants as variables. If constants don't work, just
use variables. Then you don't need to reinvent methods to achieve things for what is already
there.
http://php.net/manual/en/language.oop5.constants.php