PHP code example of victorwesterlund / functionflags

1. Go to this page and download the library: Download victorwesterlund/functionflags 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/ */

    

victorwesterlund / functionflags example snippets


use FunctionFlags/FunctionFlags

// Define global flags for use anywhere for this runtime
FunctionFlags::define([
  "MY_FLAG",
  "OTHER_FLAG"
]);

// Returns true if MY_FLAG is passed
function foo(int $flags = null): bool {
  return FunctionFlags::isset(MY_FLAG);
}

foo(MY_FLAG); // true
foo(OTHER_FLAG|MY_FLAG); // true
foo(OTHER_FLAG); // false
foo(); // false

use FunctionFlags/FunctionFlags

$flags1 = new FunctionFlags(["FOO", "BAR"]);
$flags2 = new FunctionFlags(["BAR", "BIZ"]);

// Returns true if FOO is passed and present in $flags1
function foo(int $flags = null): bool {
  return $flags2->isset(FOO);
}

foo(FOO); // true
foo(FOO|BIZ); // true
foo(BIZ); // false
foo(); // false

use FunctionFlags/FunctionFlags

FunctionFlags::define([
  "MY_FLAG",
  "OTHER_FLAG"
]);

// 1. If your function takes more than 1 argument. The "flags" variable MUST be the last.
// 2. It's recommended to make your "flags" variable default to some value if empty to make flags optional.
function foo($bar = null, int $flags = null): bool {
  return FunctionFlags::isset(MY_FLAG);
}

// Your function can now accept flags. One or many using the Union operator `|`
foo("hello world", OTHER_FLAG|MY_FLAG);