Skip to content

Conversation

@TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Sep 5, 2020

a.k.a. "The handling of #[ in php -a is terrible, are we sure we're OK with it?"

PHP treats #ini_setting=value as a call to
ini_set('ini_setting', 'value') (only in php -a) ,
and silently skips undeclared settings.

This is a problem due to #[ becoming supported attribute syntax:

  • #[Attr] const X = 123; (this is not a valid place to put an attribute)
    This does not create a constant.
  • #[Attr] function test($x=false){} also contains =.
    This does not create a function.

Instead, only treat lines starting with # as a special case
when the next character isn't [

PHP treats `#ini_setting=value` as a call to
`ini_set('ini_setting', 'value')`,
and silently skips undeclared settings.

This is a problem due to `#[` becoming supported attribute syntax:

- `#[Attr] const X = 123;` (this is not a valid place to put an attribute)
  This does not create a constant.
- `#[Attr] function test($x=false){}` also contains `=`.
  This does not create a function.

Instead, only treat lines starting with `#` as a special case
when the next character isn't `[`
@TysonAndre TysonAndre force-pushed the readline-attribute-eval branch from b3dd7f1 to 674945f Compare September 5, 2020 21:11
@php-pulls php-pulls closed this in 9439ca5 Sep 5, 2020
TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Sep 6, 2020
`php -a` treats lines starting with `#` as comments when deciding if
the provided statement is valid.

So it passed `#[MyAttr]` to the parser after the user hits enter,
causing a syntax error for multi-line statements..

With this patch, the following snippet is parsed correctly

```
php > #[Attr]
php > function x() { }
php > var_export((new ReflectionFunction('x'))->getAttributes()[0]->getName());
'Attr'
```

Followup to phpGH-6085
php-pulls pushed a commit that referenced this pull request Sep 6, 2020
`php -a` treats lines starting with `#` as comments when deciding if
the provided statement is valid.

So it passed `#[MyAttr]` to the parser after the user hits enter,
causing a syntax error for multi-line statements..

With this patch, the following snippet is parsed correctly

```
php > #[Attr]
php > function x() { }
php > var_export((new ReflectionFunction('x'))->getAttributes()[0]->getName());
'Attr'
```

Followup to GH-6085

Closes GH-6086
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants