Forum Moderators: coopster
Say you define a "constant":
define('MY_TABLE', 'some_db_table_name');
Now, say you want to use that definition in an array or object:
$foo = $bar[MY_TABLE]; // this works fine
$foo = $bar->MY_TABLE; // this DOES NOT work!
Does anyone know how to access defined variables in objects?
Thanks!
-Sned
Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.
Constants [php.net]
If you are asking how to access them when they are defined in the class itself, you can check out Class Constants [php.net]
So I've just started keeping one file for each project I work on (called something like definitions.php) which just defines all the table names and field names.
It's somewhat of a pain, doing queries like: "SELECT * FROM " . TABLENAME . " WHERE " . TABLEFIELD . " = '$val'", but it has already saved me a lot of time and effort when things change.
(Using the braces comes around when trying to get values from something like mysql_fetch_object, or getting at object data passed in a SOAP call, etc.)