2

I have a set of API keys that are constant across my codebase - I have a dev key and a live key. They are stored in their own 'keys.php' file.

I have a class that handles accessing the API and I am storing the keys in the class as class constants.

I want to be able to swap out the dev keys for live keys and access them in a static method of my class.

This works fine when I assign the class constant like this:

const API_USER_NAME = 'user_name';

But want to store the keys in their own file, and include them into the class file, and define those constants as such:

const API_USER_NAME = $user_name;

But apparently I can't assign a variable to a constant, even though that variable will not change value during execution of the program.

Is there another way I can use a variable to assign to a constant? Or at least, keep they keys in their centralized file, but still access them in the static methods of my class?

yivi
  • 42,438
  • 18
  • 116
  • 138
Abraham Brookes
  • 1,720
  • 1
  • 17
  • 32

3 Answers3

3

You could simply define your keys themselves as constants:

keys.php

const KEY_USER_NAME = 'user_name';
const KEY_USER_AGE = 'user_age';
// ...

Class

require $path_to_somewhere . '/keys.php';

class YourClass 
{
  const API_USER_NAME = KEY_USER_NAME;
  const API_USER_AGE = KEY_USER_AGE;

  // ...
}
Jeto
  • 14,596
  • 2
  • 32
  • 46
2

You can't assign a variable to a class constant, because, as the documentation states:

The value must be a constant expression, not (for example) a variable, a property, or a function call.

Variable evaluation happens at runtime, while class constant evaluation happens at compilation time. When setting the class constants, those variables are not yet ready to use.

Still, it does look like your design is wrong, IMO.

If those constants belong to the class, they should be defined there, not elsewhere. And not defined more than once (as you would be doing if you were using external values to initialize the class constants).

So either define them there directly in the class, or define them in some other file using const in a different file, and setting these constants to be globally accessible.

constants.php

const FOO = 'BAR';

consuming_file.php

require_once('constants.php');
var_dump(defined('FOO'));
// outputs bool(true)

If you are bent in using a variable to define these constants, even if defining somewhere else, you could use define(). That works with variable expressions since it is evaluated at runtime. But you can't use these in a class definition.

moreconstants.php

$bar = 'baz';
define('FOO', $bar);

echo FOO;
// outputs 'baz';

Curiously enough, constants defined using define() can be used to initialize class constants using const, no matter how the original constants where defined.

yivi
  • 42,438
  • 18
  • 116
  • 138
0

You can use the .env to solve this.

https://github.com/vlucas/phpdotenv

To achieve the result you want, you can define a .env file in a protected folder within your project and set your gitignore to not stage your .env. Then in the production environment, you can set a .env with your prodction defined constants. There's a lot of framework that meke use of .env, Laravel is a good example.