View Full Version : defining constants - undefined
Exodious
08-17-2004, 01:42 PM
Hi, I'm trying to define 2 constants to be used as array indexes, but get the following Error: Notice: Use of undefined constant ATTRIBUTENAME - assumed 'ATTRIBUTENAME'
Tried using define("ATTRIBUTENAME", 0)
Is this wrong? Is there anything I need to turn on? or am i simply being silly?
mordred
08-17-2004, 02:16 PM
No, that should work. Post the code you're using. Have you made sure the constants are actually defined in the script that runs (i.e. if you define them in an external include file, did you include it in the main script)?
Exodious
08-17-2004, 04:35 PM
I can't actually test the code atm, at work, this is what I'm trying to use them for
function nameOfClassConstructor()
{
define("ATTRIBUTENAME", 0);
define("ATTRIBUTEVALUE", 1);
}
# {{{4 adds an attribute to this node
function addAttribute($name, $value)
{
$attributeIdx = count($this->attributes);
$this->attributes[$attributeIdx][ATTRIBUTENAME] = $name;
}
The above code (or sth like it as this is a quick demo - I am aware the above array declartions need to be modded - I wanted to do sth like $this->attributes[$attributeIdx] = Array(ATTRIBUTENAME => $name, ATTRIBUTEVALUE => $value))
This sounds silly asking because a "global constant" means global, right?
But it isn't because the constants are local to the function created in...?
Thx for comments
mordred
08-17-2004, 06:55 PM
Hm, it looks a little unusual that a class method adds some constants. And yes, they will be defined globally and are available after the object's been terminated. Apart from that, your sample code works for me. Here's a working example:
class Foo {
var $attributes = array();
function Foo() {
define("ATTRIBUTENAME", 0);
define("ATTRIBUTEVALUE", 1);
}
function addAttribute($name, $value) {
$attributeIdx = count($this->attributes);
$this->attributes[$attributeIdx] = array(
ATTRIBUTENAME => $name,
ATTRIBUTEVALUE => $value
);
}
}
$test =& new Foo();
$test->addAttribute('fraggle', 'froggle');
var_dump($test);
So I can only guess the error lies somewhere else. Probably it's best if you post the non-working code in question so we can have a close look at what's going wrong. Besides, with what PHP/server version should this code run? Might make a difference.
One more thing: In the constructor, it's wise to check with defined() whether the constants have already been defined, in case you create more than one Foo object. Otherwise an error notice will be generated.
Exodious
08-17-2004, 09:25 PM
hmmm, well, I wasn't intent on using constants, but wanted to use something to make reading the selecting of array values easier to read, so ATTRIBUTENAME instead of index 0, and ATTRIBUTEVALUE instead of index 1
I tried as a test to use define() inside the funtion which created the attributes, mainly as a test, I moved the defines outside of the class and they work ok, ideally, I would like constants set which will only be available to the class which uses them... any ideas?
mordred
08-17-2004, 10:24 PM
Well, using constants instead of "magic" numbers is indeed a good idea, I do it myself when I see the need to make the code clearer. In PHP4 you can't define class-only constants. In this case, I would resort to class variables with the same name as your constants, or directly use string keys like "name" and "value" for your attribute array.
On the other hand, PHP5 gives you class constants. Here's how it would look like:
class Foo {
const ATTRIBUTENAME = 0;
const ATTRIBUTEVALUE = 1;
var $attributes = array();
function Foo() {
}
function addAttribute($name, $value) {
$attributeIdx = count($this->attributes);
$this->attributes[$attributeIdx] = array(
self::ATTRIBUTENAME => $name,
self::ATTRIBUTEVALUE => $value
);
}
}
Exodious
08-18-2004, 07:35 AM
just upgraded php version, thx
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.