PHP code example of revinate / php-getter-setter

1. Go to this page and download the library: Download revinate/php-getter-setter 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/ */

    

revinate / php-getter-setter example snippets


use Revinate\GetterSetter as gs;

$name = gs\get($data, 'name');

$name = gs\get($data, 'count', 0);

$updatedData = gs\set($data, 'count', 42);

public function testExampleJson() {
    $json = '{"name":"example","type":"json","value":22}';
    $data = json_decode($json);
    $name = gs\get($data, 'name');
    $missing = gs\get($data, 'missing');
    $this->assertEquals('example', $name);
    $this->assertNull($missing);
}

public function testExampleArrayVsObject() {
    $json      = '{"name":"example","type":"json","value":22}';
    $object    = json_decode($json);
    $array     = (array)$object;
    // The object gets updated as well
    $newObject = gs\set($object, 'type', 'Object');
    // Only the new array contains the update.
    $newArray  = gs\set($array, 'type', 'Array');
    $this->assertEquals($object, $newObject);
    $this->assertNotEquals($array, $newArray);
}

$json = $this->getEmployeeJsonData();
$data = json_decode($json);
$this->assertEquals('Arkansas', gs\get($data, 'address.state'));
$this->assertNull(gs\get($data, 'address.zip'));

class MagicAccessTestClass {

    protected $values = array();

    function __get($name) {
        return $this->values[$name];
    }
    function __isset($name) {
        return isset($this->values[$name]);
    }
    function __set($name, $value) {
        $this->values[$name] = $value;
    }
}

public function testMagicMethods() {
    $data = new MagicAccessTestClass();
    $data->first_name = 'Joe';
    $data->last_name = 'Rock';
    gs\set($data, 'profession', 'Stone cutter');
    gs\set($data, 'address', new MagicAccessTestClass());
    gs\setPathValue($data, 'address.city', 'Little Rock');
    $this->assertEquals('Joe', gs\get($data, 'first_name'));
    $this->assertEquals('Rock', gs\get($data, 'last_name'));
    $this->assertEquals('Little Rock', gs\get($data, 'address.city'));
}
json
""revinate/php-getter-setter": "~0.2"
},