PHP code example of jfcherng / php-whitelist-fluent

1. Go to this page and download the library: Download jfcherng/php-whitelist-fluent 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/ */

    

jfcherng / php-whitelist-fluent example snippets




fcherng\Utility\WhitelistFluent;

// extend your own class with WhitelistFluent

/**
 * @property int    $code the error code
 * @property array  $data the output data
 * @property string $msg  the message
 */
class ApiResponse extends WhitelistFluent
{
    /**
     * {@inheritdoc}
     */
    protected $attributes = [
        'code' => 0,
        'msg' => '',
        'data' => [],
    ];
}

$resp = new ApiResponse();

// 2 ways to get an attribute
$resp['code'];
$resp->code;

// 3 ways to set an attribute
$resp->code(200);
$resp['code'] = 200;
$resp->code = 200;

// method chaining
$resp
    ->code(500)
    ->msg('something goes wrong')
    ->data([]);

// trying to set a nonexistent attribute would throw InvalidArgumentException
$resp->nonexistent('hello');
$resp['nonexistent'] = 'hello';
$resp->nonexistent = 'hello';

// get attributes in array form
$resp->toArray();

// get attributes in json string form
$jsonFlag = JSON_PRETTY_PRINT;
$resp->toJson($jsonFlag);
json_encode($resp, $jsonFlag);
bash
# if PHP ^8.1 is used
composer quire jfcherng/php-whitelist-fluent:dev-php5