Object dot notation getter

View the Project on GitHub alrik11es/object-dot-notation

alt tag

The idea behind this library is to allow the access through
`config.port` dot notation to object data.


My main problem was when accessing API data.

        "products": [
                "name": "Shoe"

Imagine this is the result from an API. Usually to be sure that the data is what I want I’m gonna need to do:

$result = r(); // imagine this is the result from an API with the json message abobe
if(is_object($result) && property_exists($result, 'hits')){
    if(is_object($result->hits) && property_exists($result->hits, 'products')){
        $whatiwant = $result->hits->products;

This is really time consuming. I just needed a way to do something like:

$d = \Alr\ObjectDotNotation\Data::load(r());
$whatiwant = $d->get('hits.products');


You can try a demo here


Requires composer and PHP7.0+

$ composer require alrik11es/object-dot-notation


When the property is not found a null will be returned.

Take this object as example:

    "config": {
        "port": "1234",
        "url": "testurl.com"


$d = \Alr\ObjectDotNotation\Data::load($mixed);
echo $d->get('config.port'); // 1234
echo $d->{'config.port'};
echo $d->config; // ['port'=>1234 ...]

Change validation behaviour

The default validation for every sub-request checks if is object and has any properties.

You can change the last row validation behaviour to check for specific result value. Like strings or whatever.

      return is_object($data);