For different servers I have created different env variables as constants like this:
#conf.php
if (getenv('ENV') === 'a_prod' || getenv('ENV') === 'a_local') {
define("ADMIN_USERNAME", 'connector');
define("ADMIN_PASSWORD", '1234');
define("DEFAULT_LANGUAGE", 'EN');
}
if (getenv('ENV') === 'a_prod') {
define("DOMAIN", 'https://example.com');
}
if (getenv('ENV') === 'a_local') {
define("DOMAIN", 'https://127.0.0.1:8000');
}
if (getenv('ENV') === 'b_prod' || getenv('ENV') === 'b_local') {
define("ADMIN_USERNAME", 'connector');
define("ADMIN_PASSWORD", '5678');
define("DEFAULT_LANGUAGE", 'DE');
}
In different files that are executed as cronjobs I use these env variables and constants like this:
#connectorA.php
require 'connector.php';
$connector = new connector('a_prod');
and:
#connectorB.php
require 'connector.php';
$connector = new connector('b_prod');
The connector file look like this:
# connector.php
public function __construct($env)
{
echo "nEnv ApiConnector: ". $env;
putenv("ENV=$env");
require_once __DIR__ . '/config/conf.php';
}
Is it possible to run the cronjobs executing “connectorA.php” and “connectorB.php” in parallel?
Somebody said that that would not be possible as the second called cronjob will overwrite the putenv("ENV=$env"); in the connector.php constructor and that they have to be run one after the other.
Is that correct?
If so, should I better use arrays for each configuration?
On the https://www.php.net/manual/en/function.putenv.php site
it states “Adds assignment to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.”
So it should be possible, isn’t it?