PHP code example of rkgrep / attributable

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

    

rkgrep / attributable example snippets


class Foo {
    
    use rkgrep\Attributable;
    
}

class Bar {
    
    use rkgrep\Fillable;
    
}

$foo->var1 = 1;
$foo->var2(2);

echo $foo->var1;
echo $foo->var2;

$foo->get('var4', 'fallback');
$foo->get('var5', function() { return 'closure result'; });

$all = $foo->getAttributes();

$foo->viewable();
true == $foo->viewable;

$user->first_name('John')->last_name('Doe')->admin();

$user->fill(['name' => 'Admin', 'email' => '[email protected]']);

$user->fill(['name' => 'John Doe']); // Name changed, email remains untouched
$user->fill(['email' => '[email protected]'], false); // Disabled merging - old values are dropped

$user->with('password', md5('password'));

$user->with('password', '', false); // Password remains untouched

$user->with(['friends' => ['Mike', 'Dave'], 'girlfriend' => 'Jane']);
$user->with(['siblings' => [], 'girlfriend' => 'Mary'], null, false); // Overriding disabled - only siblings are touched

$post->fill(['title' => 'Lorem Ipsum'])->with('views', 5)->with('likes', 3);

class Bar implements ArrayAccess, JsonSerializable, Arrayable, Jsonable {

    use rkgrep\Attributable;

}

$bar = new Bar();
$bar->value('test');

$arrayValue = $bar['value'];
$bar['value'] = 'new';
$json = json_encode($bar); // returns '{"value": "new"}'

$array = $bar->toArray();
$json = $bar->toJson();