PHP code example of web-fu / php-dot-notation

1. Go to this page and download the library: Download web-fu/php-dot-notation 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/ */

    

web-fu / php-dot-notation example snippets


$array = [
    'foo' => [
        'bar' => 'test',
    ],
];

// Accessing an array
$dot = new Dot($array);
echo $dot->get('foo.bar'); //test

// Setting a value in an array
$dot->set('foo.bar', 'baz');
echo $array['foo']['bar']; //baz

// Accessing an object
$class = new class() {
    public string $property = 'test';
    
    public function method(): string
    {
        return 'foo';
    }
};

$dot = new Dot($class);
echo $dot->get('property'); //test
echo $dot->get('method()'); //foo

// Setting a value in an object
$dot->set('property', 'baz');
echo $class->property; //baz

$array = [];
$dot = new Dot($array);
$dot->create('foo.baz', 'test');
echo $array['foo']['baz']; // test
echo PHP_EOL; 

$class = new stdClass();
$dot = new Dot($class);
$dot->create('foo', 'test');
echo $class->foo; // test
echo PHP_EOL; 

$test = new class {
    public array $array = [
        'foo' => 'bar',
    ];
};

$dot = new Dot($test);
$dot->unset('array.foo');

var_dump(array_key_exists('foo', $test->array)); // false

// Turning an object or an array into the dotified version of it
$array = [
    'foo' => [
        'bar' => 'test',
    ],
];
$dotified = Dot::dotify($array);

echo $dotified['foo.bar']; //test

// Turning a dotified array into a normal array
$normal = Dot::undotify($dotified);

echo $normal['foo']['bar']; //test

$class = new class() {
    public function iDoSomething(): int
    {
        echo 'I Do Something ';
        return 0;
    }
}

$dot = new Dot($class);
echo $dot->get('iDoSomething()'); // I Do Something 0

$class = new class() {
    private string $property = 'test';
};

$dot = new Dot($class);
echo $dot->get('property'); //Unhandled Exception: WebFu\DotNotation\Exception\PathNotFoundException Path `property` not found

$class = new class() {
    public function thisMethodReturnsNull(): int|null
    {
        return null;
    }
    public function thisMethodDoesNotReturn(): void
    {
        //do something
    } 
};

$dot = new Dot($class);
var_dump($dot->get('thisMethodReturnsNull()')); //NULL
var_dump($dot->get('thisMethodDoesNotReturn()')); //NULL

$class = new class() {
    public function method(): int {
        return 0;
    }
};

$dot = new Dot($class);
$dot->set('method()', 20); //Unhandled Exception: WebFu\Proxy\UnsupportedOperationException Cannot set a class method
bash
composer