PHP code example of jeyroik / i-have-attributes

1. Go to this page and download the library: Download jeyroik/i-have-attributes 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/ */

    

jeyroik / i-have-attributes example snippets


$something = new class ([
    'p1' => 'v1',
    'p2' => 'v2'
]) implements IHaveAttributes {
    use THasAttributes;
};

$this->assertEquals('v1', $something->getAttribute('p1'));
$this->assertEquals('v1', $something['p1']);

$this->assertEquals('{"p1":"v1","p2":"v2"}', json_encode($something));

foreach($something as $name => $value) {
    if ($name == 'p2') {
        $this->assertEquals('v2', $value);
    }
}

$this->assertTrue(isset($something['p1']));
unset($something['p1']);
$this->assertFalse(isset($something['p1']));;

$something->__merge(['p2' => 'v2.1', 'p3' => 'v3']);

$this->assertEquals(
    ['p2' => 'v2.1', 'p3' => 'v3'],
    $something->__toArray()
);