On Fri, Mar 24, 2017 at 5:26 PM, Levi Morrison <le...@php.net> wrote:
> On Fri, Mar 24, 2017 at 9:23 AM, Andrea Faulds <a...@ajf.me> wrote: > > Hi Nikita, > > > > Nikita Popov wrote: > > > >> I'd like to add a new TOKEN_AS_OBJECT flag to token_get_all(), which > >> returns an array of PhpToken objects, rather than the mix of plain > strings > >> and arrays we currently have. The PhpToken class is defined as: > >> > >> class PhpToken { > >> public $type; > >> public $text; > >> public $line; > >> } > > > > > > Rather than adding a flag to token_get_all() to return objects, you could > > potentially instead make an equivalent static method on PhpToken > > (PhpToken::getAll() perhaps). That would avoid mixing “object-oriented” > and > > “procedural” styles, though I don't know if it matters. It seems cleaner > to > > me. > > > > Thanks! > > > > -- > > Andrea Faulds > > https://ajf.me/ > > > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > I prefer a distinct function/method from `token_get_all`. I don't see > the value in having a return value that differs so much based on a > passed flag. I don't care so much about a function vs static method; I > just oppose the additional parameter to `token_get_all`. > > I like the idea very much overall aside from that detail. > token_get_all() already has a $flags parameter controlling its output. Currently only the TOKEN_PARSE flag is supported, which changes the interpretation of certain tokens. In a previous thread on this topic Sara suggested another flag (something like TOKEN_ALWAYS_ARRAY) which makes the output of token_get_all() consistently use arrays. I think handling this as a flag is entirely reasonable, and by analogy, handling the object mode as a flag is also reasonable. In the end, these are just minor variations on the output format (minor relative to the very complex primary functionality of the function: lexing). Another precedent is the json_decode() option to use arrays or objects in the return value. Regards, Nikita