1. Go to this page and download the library: Download greenflags/greenflags-php 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/ */
greenflags / greenflags-php example snippets
use GreenFlags\Client;
use GreenFlags\Cache\ApcuCache;
$flags = new Client(
url: 'https://app.greenflags.dev',
apiToken: getenv('GREENFLAGS_API_TOKEN'),
cache: new ApcuCache(),
);
$flags->sync(60); // cache hit: 0 requests · cache miss: 1 billable read
if ($flags->isEnabled('new-checkout')) {
// ship it
}
// 1. Load the snapshot (cache-first; see sync() above)
$flags->sync(60);
// 2. Read flags — always from memory, never hits the network
$enabled = $flags->isEnabled('my-feature'); // bool sugar
$theme = $flags->getFlag('theme', 'light'); // string with default
$limit = $flags->getFlag('rate-limit', 100); // number with default
$config = $flags->getFlag('config', []); // json/array with default
// 3. List everything available
$all = $flags->getAllFlags(); // list<Flag>
$snapshot = $flags->getSnapshot(); // array<string, Flag>
// 4. Force a network refresh (1 billable read) — e.g. from a cron/artisan command
$flags->refresh(cacheTtlSeconds: 60);
// app/Providers/AppServiceProvider.php
use GreenFlags\Client;
use GreenFlags\Cache\SnapshotCacheInterface;
use Illuminate\Support\Facades\Cache;
final class LaravelSnapshotCache implements SnapshotCacheInterface
{
public function get(): ?string
{
return Cache::get('greenflags.snapshot');
}
public function set(string $snapshotJson, int $ttlSeconds): void
{
Cache::put('greenflags.snapshot', $snapshotJson, $ttlSeconds);
}
}
public function register(): void
{
$this->app->singleton(Client::class, fn () => new Client(
url: config('services.greenflags.url', 'https://app.greenflags.dev'),
apiToken: config('services.greenflags.token'),
cache: new LaravelSnapshotCache(),
));
}
// anywhere (controller, middleware, Blade view via injection)
$flags = app(GreenFlags\Client::class);
$flags->sync(60);
if ($flags->isEnabled('new-checkout')) { ... }
/**
* Plugin Name: GreenFlags
*/
use GreenFlags\Client;
use GreenFlags\Cache\SnapshotCacheInterface;
{
$value = get_transient('greenflags_snapshot');
return is_string($value) ? $value : null;
}
public function set(string $snapshotJson, int $ttlSeconds): void
{
set_transient('greenflags_snapshot', $snapshotJson, $ttlSeconds);
}
}
function gf(): Client
{
static $client = null;
if ($client === null) {
$client = new Client(
url: 'https://app.greenflags.dev',
apiToken: defined('GREENFLAGS_TOKEN') ? GREENFLAGS_TOKEN : '', // wp-config.php
cache: new WpTransientSnapshotCache(),
);
try {
$client->sync(60);
} catch (\GreenFlags\GreenFlagsException) {
// Fail-open: the store keeps working with defaults.
}
}
return $client;
}
function gf_is_enabled(string $key): bool
{
return gf()->isEnabled($key);
}
// Toggle a checkout feature
add_action('woocommerce_before_checkout_form', function () {
if (gf_is_enabled('holiday-banner')) {
wc_print_notice(gf()->getFlag('holiday-banner-text', ''), 'notice');
}
});