alrik11es/object-dot-notation

Improve library efficiency

alrik11es opened this issue · 0 comments

I've been messing around this library from time to time. In the meantime I've discovered an interesting page talking about this topic in PHP.

This is very interesting to improve the speed of this library that is something really needed because with big datasets it becomes too slow.

$res = array_reduce(explode('.', $request_field), function ($o, $p) {
                            return $o->$p ?? null;
                            }, $row);

There's always the option to use the eval to improve the efficiency and with a proper regex should be very secure. But I think that the solution based on array_reduce is enough for speeding up the things.

Anyways this is my approach to eval which is obviously a not recommended option.

if(preg_match('/^[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$/', $request_field)) {
    eval('$res = $row->' . str_replace('.', '->', $request_field) . ';');
}