Hey Jesse,

On Tue, Apr 6, 2021, 03:00 Jesse Rushlow <j...@rushlow.dev> wrote:

> Sure thing -
>
> In Symfony's MakeBundle we have a command "php bin/console make:command".
> This ultimately generates a "App\Console\XyzUserlandCommand::class" that
> allows the developer to customize in order to perform some task within
> their app. e.g. run persistence operations, generate a backup, etc..
>
> What we do internally to generate that userland class is determine which
> features to include by checking the PHP version, dependencies available,
> etc.. As an example, when the user runs "make:command"; we check if they
> have PHP 8 && if "class_exists(Symfony\.....\Attributes::class)" - if both
> of those === true, we import the attribute class and generate the needed
> attributes for the command. Otherwise we would fall back to using
> annotations if the user is running PHP 7. Where I ran into trouble was the
> Attributes::class (defines the attributes to be used in userland) utilized
> constructor property promotion (which is a pretty sweet addition to 8 imo).
>
> Up to this point if it's possible, we generally pick a
> class/interface/method that introduces new functionality (Symfony
> feature/bugfix) and call one of the \*_exists(class/method/interface)
> functions in a conditional to determine if we can indeed use that feature.
> But because in this particular case the Attributes::class also uses
> constructor property promotion, when calling
> class_exists(Attributes::class) in PHP 7 we get:
>
>
> ParseError: syntax error, unexpected 'public' (T_PUBLIC), expecting
> variable (T_VARIABLE)
>
> The work around was to "$canUseAttributes = 80000 <= \PHP_VERSION_ID && \
> class_exists(AsCommand::class)"
>
> All is fine and well, but this in my opinion, this feels like bad mojo from
> a developers standpoint. I must now need to be aware of the exact
> implementation details of the object I'm checking before I can even check
> if it exists or not. I would expect any of the \*_exists() to tell me if
> the class/method/interface exists regardless of the implementation. My
> first thought would be if the implementation is not compatible with the PHP
> version some sort of \RuntimeException::class would be thrown by PHP. But
> that is where my knowledge of PHP internals is disconnected from developing
> software in PHP.
>
> I created a simple reproducer repo
> https://github.com/rushlow-development/attribute-test (forgive the repo
> name, it really has nothing to do with attributes.) and for an even simpler
> example https://3v4l.org/4cMW4
>
> I apologize in advance for the wall of text - but I'm curious if there is a
> way to either improve our native functions, improve the docs, and/or create
> new functions for use cases like this.
>

`*_exists()` functions triggering autoloading is intentional and required.

Perhaps you are trying to reflect in unsafe symbols, which explicitly need
to skip autoloading, as you are not yet sure about what you will encounter.

This is exactly what https://github.com/Roave/BetterReflection was built
for: perhaps you can help in getting up to speed with 8.x featured, and
then it can be used in the bundle internals (rather than relying on runtime
side-effects)?

>

Reply via email to