PHP code example of jstewmc / detect-environment

1. Go to this page and download the library: Download jstewmc/detect-environment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jstewmc / detect-environment example snippets


namespace Jstewmc\DetectEnvironment;

// define our possible environments
$values = [
    'development' => 'foo',
    'testing'     => 'bar',
    'staging'     => 'baz',
    'production'  => 'qux'
];

// put the environment variable
putenv('APP_ENV=foo');

// instantiate the service
$service = new DetectEnvironment('APP_ENV', $values);

// detect the environment
$service->isDevelopment();  // returns true
$service->isTesting();      // returns false
$service->isStaging();      // returns false
$service->isProduction();   // returns false

// get the environment's name
$service();  // returns "development"

namespace Jstewmc\DetectEnvironment;

putenv('APP_ENV=foo');

$service = new DetectEnvironment(
    'APP_ENV', 
    [
        'development' => 'foo',
        'testing'     => 'bar',
        'staging'     => 'baz',
        'production'  => 'qux'
    ]
);

namespace Jstewmc\DetectEnvironment;

putenv('APP_ENV=foo');

$service = new DetectEnvironment(
    'APP_ENV', 
    [
        'development' => 'foo',
        'testing'     => 'bar',
        'staging'     => 'baz',
        'production'  => 'qux'
    ]
);

$service->isDevelopment();  // returns true
$service->isTesting();      // returns false
$service->isStaging();      // returns false
$service->isProduction();   // returns false
$service->foo();            // throws exception (MUST start with "is")
$service->isFoo();          // throws exception (MUST be valid environment name)