# TOKEN_AS_OBJECT for token_get_all()

- Thread: https://externals.io/message/98610
- Original archive: https://news-web.php.net/php.internals/98610
- Started by Nikita Popov on 2017-03-23T17:16:31+00:00
- Messages: 15
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #98610 — Nikita Popov — 2017-03-23T17:16:31+00:00

URL: https://externals.io/message/98610#98610

Hi internals,

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;
}

This has been previously suggested and implemented by Rouven Weßling [1],
I've just ported this feature to master and optimized the implementation
[2].

Apart from being a nicer interface than the current format, an additional
advantage is that the  TOKEN_AS_OBJECT mode uses about 1.8x less memory and
is 1.4x times faster.

Are there any objections to this change?

Regards,
Nikita

[1]: https://github.com/php/php-src/pull/1727
[2]: https://github.com/php/php-src/pull/2430

---

## #98612 — j.tvr@centrum.cz — 2017-03-23T22:25:44+00:00

URL: https://externals.io/message/98610#98612 · In reply to: #98610

On Thu, 23 Mar 2017 18:16:31 +0100, Nikita Popov <nikita.ppv@gmail.com>  
wrote:

> Hi internals,
>
> 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;
> }
>
> This has been previously suggested and implemented by Rouven Weßling [1],
> I've just ported this feature to master and optimized the implementation
> [2].
>
> Apart from being a nicer interface than the current format, an additional
> advantage is that the  TOKEN_AS_OBJECT mode uses about 1.8x less memory  
> and
> is 1.4x times faster.
>
> Are there any objections to this change?
>
> Regards,
> Nikita
>
> [1]: https://github.com/php/php-src/pull/1727
> [2]: https://github.com/php/php-src/pull/2430

Regarding memory - would it be possible to return iterator instead of  
array?

Regards,
Jan Tvrdik

---

## #98613 — Sara Golemon — 2017-03-23T23:33:31+00:00

URL: https://externals.io/message/98610#98613 · In reply to: #98612

On Thu, 23 Mar 2017 18:16:31 +0100, Nikita Popov <nikita.ppv@gmail.com>
> 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;
> }
>
> This has been previously suggested and implemented by Rouven Weßling [1],
> I've just ported this feature to master and optimized the implementation
> [2].
>
>
Yeah, IIRC the more recent discussion ended at "Oh, and this! And
this! And this!" which eventually went nowhere (largely my ADD).  IMO
there's no harm in adding this, and the class format seems entirely
reasonable.

If I may bikeshed a TINY bit, I'd ask that all tokens return as
objects, rather than char|PhpToken similar to the current char|array
format we have. (Maybe that's in the PR, I haven't looked at either)



> Regarding memory - would it be possible to return iterator instead of array?
>
That has some hairy edges to it since the lexer isn't actually
reentrant within a given thread. Image the following:

foreach (token_get_all($code, TOKEN_AS_ITERATOR) as $token) {
  $moretokens = token_get_all($morecode, $whateverflags); // <--- Here
be broken dreams and crying unicorns
}

Worse still if you have parallel iterators.

That's probably fixable, but it's a much heavier refactor and one that
should probably have an RFC.

-Sara

---

## #98614 — Nikita Popov — 2017-03-23T23:44:11+00:00

URL: https://externals.io/message/98610#98614 · In reply to: #98613

> On Thu, 23 Mar 2017 18:16:31 +0100, Nikita Popov <nikita.ppv@gmail.com>
> > […]
> strings
> > […]
> Yeah, IIRC the more recent discussion ended at "Oh, and this! And
> this! And this!" which eventually went nowhere (largely my ADD).  IMO
> there's no harm in adding this, and the class format seems entirely
> reasonable.
>
> If I may bikeshed a TINY bit, I'd ask that all tokens return as
> objects, rather than char|PhpToken similar to the current char|array
> format we have. (Maybe that's in the PR, I haven't looked at either)
>

That's how it's currently implemented, yes. The output is PhpToken[].


> 
> > […]
> array?
> > […]
> That has some hairy edges to it since the lexer isn't actually
> reentrant within a given thread. Image the following:
>
> foreach (token_get_all($code, TOKEN_AS_ITERATOR) as $token) {
>   $moretokens = token_get_all($morecode, $whateverflags); // <--- Here
> be broken dreams and crying unicorns
> }
>
> Worse still if you have parallel iterators.
>
> That's probably fixable, but it's a much heavier refactor and one that
> should probably have an RFC.
>

The lexer is reentrant as long as the lexer state is saved and restored
every time the iterator is advanced -- we already need to support reentry
because, for various unpleasant reasons, it is actually possible that a
compilation is triggered while another is still in progress...

But yes, this is definitely a much more complicated change, and one I would
not consider to be particularly useful. In my experience most interesting
uses of `token_get_all()` are not compatible with a simple iterator, or would
eschew it because the iterator would in all likelihood perform worse.

Nikita

---

## #98616 — Sebastian Bergmann — 2017-03-24T06:22:15+00:00

URL: https://externals.io/message/98610#98616 · In reply to: #98613

Am 24.03.2017 um 00:33 schrieb Sara Golemon:
> If I may bikeshed a TINY bit, I'd ask that all tokens return as
> objects, rather than char|PhpToken similar to the current char|array
> format we have.

Yes, please. That would basically/probably/hopefully make php-token-stream
superfluous.

---

## #98617 — Andrea Faulds — 2017-03-24T15:23:42+00:00

URL: https://externals.io/message/98610#98617 · In reply to: #98610

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/

---

## #98618 — David Rodrigues — 2017-03-24T15:33:55+00:00

URL: https://externals.io/message/98610#98618 · In reply to: #98617

Em 24 de mar de 2017 12:24 PM, "Andrea Faulds" <ajf@ajf.me> escreveu:

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.

I liked your suggestion. Maybe just change to something like
SplTokenizer::parse(): SplToken[]


Thanks!

-- 
Andrea Faulds
https://ajf.me/

---

## #98619 — Levi Morrison — 2017-03-24T16:26:32+00:00

URL: https://externals.io/message/98610#98619 · In reply to: #98617

> Hi Nikita,
>
> Nikita Popov wrote:
>
> > […]
>
>
> 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/
>
>
> --
> 
> 

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.

---

## #98621 — Fleshgrinder — 2017-03-24T18:35:16+00:00

URL: https://externals.io/message/98610#98621 · In reply to: #98617

> Hi Nikita,
> 
> Nikita Popov wrote:
> 
> > […]
> 
> 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!
> 

I am also against adding another flag because it violates the single
responsibility principle. However, adding a `getAll` method to the
`PhpToken` data class also seems very wrong, because it violates the
single responsibility once again.

The real and proper solution is to have an actual PHP Lexer that is
capable of creating a token stream.

    class Lexer {
        public function __construct(string $source);
        public static function fromFile(string $path): self;
        public function tokenize(): TokenStream;
    }

    final class TokenStream implements IteratorAggregate {
        public function getIterator(): Generator<Token>
        public function toArray(): Token[]
    }

    final class Token {
        // Ideally this would be an enum, but...
        public const OPEN_TAG;
        public const CLOSE_TAG;
        // ...

        // I hope these are not mutable!
        public int $category;  // type
        public string $lexeme; // text
        public int $line;
        public int $column;    // we don't have that :(

        /** @see token_name */
        public function getCategoryName(): string;

        // We could add `is*` methods for the various categories here.
        public function isOpenTag(): bool;
        public function isCloseTag(): bool;
        // ...
    }

With this in place, we're ready for the future. The `TokenStream` can
easily use a generator over the internal array. Or we offer the
`toArray` method only at the beginning. Users will have to call it, but
that overhead is tiny, considering that we can extend upon it in the
future without introducing any kind of breaking change.

Obviously this could go into the namespace `PHP\Parser`, or we prefix
everything with `PHP` (I'd be for the former).

On a side note, going for `Php` instead of `PHP` is inconsistent with
the naming that is currently dominating the PHP core:

https://secure.php.net/manual/en/indexes.functions.php

There are already some things that violate it, e.g. `Phar` instead of
`PHAR`, but most things keep acronyms intact (`DOM`, `XML`, `PDO`, ...).
Introducing even more inconsistency to PHP seems like a very bad idea to
me. I know that this is considered bikeshedding by many people, but
consistency is very important and we are already lacking it on too many
levels as it is.

-- 
Richard "Fleshgrinder" Fussenegger

---

## #98622 — guilhermeblanco@gmail.com — 2017-03-24T18:43:25+00:00

URL: https://externals.io/message/98610#98622 · In reply to: #98621

Thanks Fleshgrinder!

You expressed exactly what I thought, but was too busy... cough... lazy to
put in an email.

Cheers,



> 
> > […]
>
> I am also against adding another flag because it violates the single
> responsibility principle. However, adding a `getAll` method to the
> `PhpToken` data class also seems very wrong, because it violates the
> single responsibility once again.
>
> The real and proper solution is to have an actual PHP Lexer that is
> capable of creating a token stream.
>
>     class Lexer {
>         public function __construct(string $source);
>         public static function fromFile(string $path): self;
>         public function tokenize(): TokenStream;
>     }
>
>     final class TokenStream implements IteratorAggregate {
>         public function getIterator(): Generator<Token>
>         public function toArray(): Token[]
>     }
>
>     final class Token {
>         // Ideally this would be an enum, but...
>         public const OPEN_TAG;
>         public const CLOSE_TAG;
>         // ...
>
>         // I hope these are not mutable!
>         public int $category;  // type
>         public string $lexeme; // text
>         public int $line;
>         public int $column;    // we don't have that :(
>
>         /** @see token_name */
>         public function getCategoryName(): string;
>
>         // We could add `is*` methods for the various categories here.
>         public function isOpenTag(): bool;
>         public function isCloseTag(): bool;
>         // ...
>     }
>
> With this in place, we're ready for the future. The `TokenStream` can
> easily use a generator over the internal array. Or we offer the
> `toArray` method only at the beginning. Users will have to call it, but
> that overhead is tiny, considering that we can extend upon it in the
> future without introducing any kind of breaking change.
>
> Obviously this could go into the namespace `PHP\Parser`, or we prefix
> everything with `PHP` (I'd be for the former).
>
> On a side note, going for `Php` instead of `PHP` is inconsistent with
> the naming that is currently dominating the PHP core:
>
> https://secure.php.net/manual/en/indexes.functions.php
>
> There are already some things that violate it, e.g. `Phar` instead of
> `PHAR`, but most things keep acronyms intact (`DOM`, `XML`, `PDO`, ...).
> Introducing even more inconsistency to PHP seems like a very bad idea to
> me. I know that this is considered bikeshedding by many people, but
> consistency is very important and we are already lacking it on too many
> levels as it is.
>
> --
> Richard "Fleshgrinder" Fussenegger
>
> --
> 
> 
>
>


-- 
Guilherme Blanco
Senior Technical Architect at Huge Inc.

---

## #98623 — Nikita Popov — 2017-03-24T19:04:26+00:00

URL: https://externals.io/message/98610#98623 · In reply to: #98619

> 
> > […]
> strings
> > […]
> and
> > […]
> to
> > […]
>
> 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

---

## #98624 — Niklas Keller — 2017-03-24T19:15:16+00:00

URL: https://externals.io/message/98610#98624 · In reply to: #98621

2017-03-24 19:35 GMT+01:00 Fleshgrinder <php@fleshgrinder.com>:

> 
> > […]
>
> I am also against adding another flag because it violates the single
> responsibility principle. However, adding a `getAll` method to the
> `PhpToken` data class also seems very wrong, because it violates the
> single responsibility once again.
>
> The real and proper solution is to have an actual PHP Lexer that is
> capable of creating a token stream.
>
>     class Lexer {
>         public function __construct(string $source);
>         public static function fromFile(string $path): self;
>         public function tokenize(): TokenStream;
>     }
>

If PhpToken::getAll() violates the SRP, then Lexer::fromFile() is just the
same sort of violation.

Regards, Niklas


>     final class TokenStream implements IteratorAggregate {
>         public function getIterator(): Generator<Token>
>         public function toArray(): Token[]
>     }
>
>     final class Token {
>         // Ideally this would be an enum, but...
>         public const OPEN_TAG;
>         public const CLOSE_TAG;
>         // ...
>
>         // I hope these are not mutable!
>         public int $category;  // type
>         public string $lexeme; // text
>         public int $line;
>         public int $column;    // we don't have that :(
>
>         /** @see token_name */
>         public function getCategoryName(): string;
>
>         // We could add `is*` methods for the various categories here.
>         public function isOpenTag(): bool;
>         public function isCloseTag(): bool;
>         // ...
>     }
>
> With this in place, we're ready for the future. The `TokenStream` can
> easily use a generator over the internal array. Or we offer the
> `toArray` method only at the beginning. Users will have to call it, but
> that overhead is tiny, considering that we can extend upon it in the
> future without introducing any kind of breaking change.
>
> Obviously this could go into the namespace `PHP\Parser`, or we prefix
> everything with `PHP` (I'd be for the former).
>
> On a side note, going for `Php` instead of `PHP` is inconsistent with
> the naming that is currently dominating the PHP core:
>
> https://secure.php.net/manual/en/indexes.functions.php
>
> There are already some things that violate it, e.g. `Phar` instead of
> `PHAR`, but most things keep acronyms intact (`DOM`, `XML`, `PDO`, ...).
> Introducing even more inconsistency to PHP seems like a very bad idea to
> me. I know that this is considered bikeshedding by many people, but
> consistency is very important and we are already lacking it on too many
> levels as it is.
>
> --
> Richard "Fleshgrinder" Fussenegger

---

## #98627 — Fleshgrinder — 2017-03-24T20:34:06+00:00

URL: https://externals.io/message/98610#98627 · In reply to: #98624

> If PhpToken::getAll() violates the SRP, then Lexer::fromFile() is just the
> same sort of violation.
> 
> Regards, Niklas
> 

This is kind of true in PHP, because we are lacking proper abstractions
for path and file handling. It is most certainly not true in many other
languages. Usually such named constructors or factory methods are just
short-hands to trigger a series of other behavior that abides the SRP.

    throws InvalidPathException
    throws IOException
    public static function fromFile(string $path): self {
        return new static(new File(new Path($path))->getContents());
    }

There is not logic at all inside this method, it is only there for
convenience to spare you from writing this over and over and over and
over ... again. In the PHP case the whole thing directly explodes in
complexity:

    public static function fromFile(string $path): self {
        // Already questionable if we can actually pass that string
        // to file_get_contents since it might be a remote file, or
        // complete garbage that does not even remotely resemble a
        // path ... who knows ...
        $contents = file_get_contents($path);

        if ($contents === false) {
            // Let's just hope there is no other error in here from
            // a previous error ...
            $error = `error_get_last()`;
            // ...
        }

        return new static($contents);
    }

I shortened the whole thing because it is to cumbersome to write all of
this without an IDE. So yes it violates the SRP in classic PHP, but no
it does not necessarily violate SRP per se.

Also note that adding `Token::getAll` is more than weird from a logical
point of view too. Since `Token` represents a single thing, whereas
`getAll` by logic belongs to some kind of collection of things.

Note that if we go further with the whole thing that we wouldn't even
want to deal with a string in the constructor, but rather a `Reader`,
`BufferedReader`, or `SeekableReader`. A `File` instance most certainly
would comply with that:

    final class Lexer {

        public function __construct(SeekableReader $source);

        public static function fromFile(string $path): self {
            return new static(new File(new Path($path)));
        }

    }

Now in case we want to work with a string directly, e.g. unit tests, we
would need a seekable reader that encapsulates that string, e.g. a `Cursor`.

    new Lexer(new Cursor('<?php echo "foo";'));

This usecase might not be as common as the one where we want to tokenize
a file's content, thus, we do not add a short-hand for it. However, in
case it would be so common that it makes sense to add, we should add it.

    final class Lexer {

        // Same as before...

        public static function fromString(string $source): self {
            new static(new Cursor($source));
        }

    }

Once again, no logic, no branching, no hardcoded strategies. It does one
thing and one thing only: sparing us from re-typing the same construct
over and over again. DRY is one of the most important principles of them
all after all.

Another question to ask before adding such methods lightly is whether we
can justify the additional dependency. Depending on `File` and `Path` is
most certainly never a problem, after all, we need the file system at
all times. The `Cursor` on the other hand might be debatable, as it
might reside in a special component that is not necessarily available
just because the `Lexer` is.

I could go on and on and on now but I'm getting kind of off topic now. ?

-- 
Richard "Fleshgrinder" Fussenegger

---

## #98634 — Nikita Popov — 2017-03-25T12:18:59+00:00

URL: https://externals.io/message/98610#98634

> On Fri, Mar 24, 2017 at 1:04 PM, Nikita Popov <nikita.ppv@gmail.com>
> wrote:
> > […]
> the
> > […]
> as
> > […]
> as a
> > […]
> the
> > […]
> of
> > […]
> use
> > […]
>
> Whether `TOKEN_PARSE` is passed or not the return type is Array<char |
> Array>. You are proposing an entirely different return structure of
> Array<PhpToken>. What are the benefits of reusing the same function
> have? To me adjusting a parameter and getting a totally different
> structure is not a benefit, so there must be some other unspecified
> advantage you haven't articulated yet. If there is a benefit to it
> great; please say so. Otherwise I don't see why you want to make an
> existing function more complicated when you can just make a different
> function.
>

(cc'ing back internals)

The benefit that I was alluding to in my previous reply, though failed to
state explicitly, is essentially forward compatibility, in the sense of
robustness to extension. We already have an existing flag controlling
behavior, I am proposing to add another (whether or not it be implemented
as an actual flag -- it is a flag in the sense of a combinatorial boolean
option), and it is not unreasonable to expect that more such options may
appear in the future. It is inconsistent if some of these options are
implemented as flags, while others are separate functions. Implementing
everything as separate functions results in combinatorial explosion. Having
the variants `token_get_all()`, token_get_all_as_object(),
token_get_all_with_positions(), token_get_all_as_object_with_positions() is
certainly not an enviable circumstance to be in. Of course, you may argue
that "as object" and "with positions" are different because one changes the
return type while the other doesn't (in the particularly limited typesystem
you happen to be using -- a more accurate description would be array<int,
char|tuple<int, string, int>> vs array<int, char|tuple<int, string, int,
int>>), so let's pick another example that can not be implemented as flag:
The token_get_all_from_file() variant that Fleshgrinder has suggested
(under a different name). While I do not personally believe this to be
useful, there are some technical arguments that can be made in favor of
having such a function (our lexer is capable of lexing directly from a file
more efficiently). Now again, if we implement the AS_OBJECT mode using a
separate function, this would require having all of `token_get_all()`,
token_get_all_as_object(), token_get_all_from_file() and
token_get_all_from_file_as_object(). Whether or not any of these specific
examples are going to happen, I hope this serves to illustrate the general
hazard of implementing flags (in the sense of combinatorial boolean
options) as separate functions.

As a more meta-level commentary: While I can appreciate the philosophy of
preferring new functions over extended arguments, this is certainly not the
first time this discussion has been had on this list. The end result tends
to be the same: Whatever your personal philosophy might be, the existing
corpus of PHP functions has a very strong preference towards extending
existing functions rather than introducing new ones.

Overall, I must say that this thread has left me rather disappointed. I was
harboring the misguided hope that it might be possible to introduce a
simple, self-contained improvement, that resolves a major ergonomics pain
point in ext/tokenizer, while also improving performance and memory usage.
Essentially, a win on all fronts. What I get instead is a continued
escalation of this simple change, first into the addition of a new function
(which I disagree with, but which is still a feasible suggestion), then
into the addition of a complex object-oriented system involving three
classes, at least six methods, iterators and even managing to involve the
question of namespaced core functionality, a well known and extensively
discussed unresolved question! What am I supposed to do with such
suggestions? I will obviously not waste my time discussing the design of
such a system, let alone implementing it in its glorious complexity.

This is the point where I abandon this particular idea, as clearly my
expectations of the scope of this change do not line up with those of
others. I really wish people could get their priorities straight.

Nikita

---

## #98636 — Fleshgrinder — 2017-03-25T13:37:52+00:00

URL: https://externals.io/message/98610#98636 · In reply to: #98634

> Overall, I must say that this thread has left me rather disappointed. I was
> harboring the misguided hope that it might be possible to introduce a
> simple, self-contained improvement, that resolves a major ergonomics pain
> point in ext/tokenizer, while also improving performance and memory usage.
> Essentially, a win on all fronts. What I get instead is a continued
> escalation of this simple change, first into the addition of a new function
> (which I disagree with, but which is still a feasible suggestion), then
> into the addition of a complex object-oriented system involving three
> classes, at least six methods, iterators and even managing to involve the
> question of namespaced core functionality, a well known and extensively
> discussed unresolved question! What am I supposed to do with such
> suggestions? I will obviously not waste my time discussing the design of
> such a system, let alone implementing it in its glorious complexity.
> 
> This is the point where I abandon this particular idea, as clearly my
> expectations of the scope of this change do not line up with those of
> others. I really wish people could get their priorities straight.
> 
> Nikita
> 

I think the general problem is that PHP is developing in all directions.
One of them is the "high-level language with low-level primitives"
direction that we have on the procedural side (yours), and the
"high-level language with high-level abstractions" direction that we
have on the OO/functional side (mine).

The approach you would like to take might be more pragmatic, and yield a
result faster. However, it is yet just another function where people are
required to first consult the documentation to actually find what is
possible to do. Especially if the flags keep on piling up. The returned
value is also always different, hence, every time one encounters this
function again she must go back to the documentation to check what it is
actually returning in this particular call. Just have a look once again
at the various highly complex return type combinations you just posted.

Continuing on the road of cheap wins, or getting features in fast to
solve some pain points (which you definitely are addressing here, no
argument there), directly translates to continuing on the road of no
coherent design of the overall language; in other words keeping it
C-like procedural. Whereas at the same time we have a HUGE shift towards
OO and functional in PHP since many years.

The things you are addressing with a combinatorial explosion is exactly
why I was proposing a proper OO design which addresses multiple issues:

- Easy exploration of possibilities via code completion (#no-doc).
- Clear separation of concerns.
- Naming that aids findability and comprehensibility.
- Dependency inversion and injection possibilities.
- Extension and forward compatibility is easy without breaking anything.
- Clear simple, single return types.

I understand your frustration, but I must keep my priorities straight.
Even if that means that I piss off one of the most important PHP
contributors. ?

PS: Please remember that I clearly stated that `fromFile` and the
iterator support can be added at a later stage. The same would be true
for any other additional functionality besides the one you are proposing.

**TL;DR** My priorities are straight: PHP is a high-level not low-level
language (some parties most definitely disagree here with me) and should
offer easy to understand, beginner friendly abstractions that are hard
to use incorrectly. The proposed change is the opposite and closer to C
than to a high-level language.

-- 
Richard "Fleshgrinder" Fussenegger
