Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That smells to me: the path argument given to "get_path" is a string of components, separated by "/". The first thing get_path does is split the path apart at "/".

Why doesn't it just take an array? That way, it would be a simple reduction:

    function get_path($data, $path) {
      return array_reduce(
        $path,
        function($result, $index) {
          return (is_array($result) && isset($result[$index]))? $result[$index] : NULL;
        },
        $data);
    }
Unfortunately PHP's function namespace is separate from its value namespace. We can't define "get_path" using a couple of combinators :(

Also, requiring 'strings without slashes' is a pretty bad idea in PHP in particular, since it's meant for Web programming. This means a) we tend to have / in strings due to URLs and b) someone will inevitably pass user input as one of these components, which would allow a very limited form of code injection attack.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: