PHP code example of turtlemilitia / laravel-featureflags
1. Go to this page and download the library: Download turtlemilitia/laravel-featureflags 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/ */
turtlemilitia / laravel-featureflags example snippets
use FeatureFlags\Facades\Feature;
if (Feature::active('dark-mode')) {
// Show dark mode UI
}
use FeatureFlags\Facades\Feature;
// Check if a flag is active (boolean)
if (Feature::active('dark-mode')) {
// Show dark mode UI
}
// Get flag value (supports string, number, JSON)
$limit = Feature::value('api-rate-limit');
// Get all flags
$flags = Feature::all();
// Monitor critical code paths (tracks errors automatically)
$result = Feature::monitor('new-payment-flow', fn ($enabled) =>
$enabled ? $this->newPayment() : $this->legacyPayment()
);
// Track conversions for A/B analysis
Feature::trackConversion('purchase', $user, ['revenue' => 99.99]);
use FeatureFlags\Contracts\HasFeatureFlagContext;
class User extends Authenticatable implements HasFeatureFlagContext
{
public function toFeatureFlagContext(): array
{
return [
'id' => $this->id,
'email' => $this->email,
'plan' => $this->subscription?->plan,
];
}
}
// Team, Organization, etc.
Feature::active('premium-feature', $team);
// User sees their assigned variant automatically
$buttonColor = Feature::value('checkout-button-color');
// Returns 'blue', 'red', or 'green' based on user's bucket
// User sees the checkout button (variant: 'red')
$color = Feature::value('checkout-button-color');
// Later in the same request or session...
Feature::trackConversion('purchase', $user, ['revenue' => 99.99]);
// Conversion is automatically attributed to 'checkout-button-color' = 'red'
Feature::sync();
Feature::flush(); // Clear cache
'fallback' => [
'behavior' => 'cache', // 'cache', 'default', or 'exception'
'default_value' => false, // Used when behavior is 'default'
],
use FeatureFlags\Facades\Feature;
// Simple boolean flag
Feature::shouldReceive('active')
->with('my-flag')
->andReturn(true);
// Flag with context
Feature::shouldReceive('active')
->with('premium-feature', Mockery::any())
->andReturn(false);
// Value flags
Feature::shouldReceive('value')
->with('api-rate-limit')
->andReturn(100);
public function test_rollout_is_consistent(): void
{
$context = new Context('user-123', []);
$first = Feature::active('gradual-rollout', $context);
$second = Feature::active('gradual-rollout', $context);
// Same context always gets same result
$this->assertEquals($first, $second);
}
use FeatureFlags\Facades\Feature;
// Flags work immediately (bucketing happens, telemetry is held)
if (Feature::active('new-checkout')) {
// Show new checkout
}
// When user accepts analytics/cookies:
Feature::grantConsent(); // Flushes held events, sets consent cookie
// If user declines:
Feature::discardHeldTelemetry(); // Clears queued events without sending
// To revoke consent later:
Feature::revokeConsent(); // Future telemetry will be held again
// Check current state:
Feature::isHoldingTelemetry(); // true if holding without consent