# Scalar Type Declaration Syntax Weirdness

- Thread: https://externals.io/message/89375
- Original archive: https://news-web.php.net/php.internals/89375
- Started by Sebastian Bergmann on 2015-11-24T15:10:54+00:00
- Messages: 27
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/89375.md?page=2
- 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

---

## #89375 — Sebastian Bergmann — 2015-11-24T15:10:54+00:00

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

The following is currently valid PHP 7 code

      <?php
      function a(\int $i) {}

  Is it intentional that the \ in front of the "int" is allowed? IMHO, this
  confusing notation must not be allowed.

---

## #89376 — Ryan Pallas — 2015-11-24T15:27:34+00:00

URL: https://externals.io/message/89375#89376 · In reply to: #89375

On Tue, Nov 24, 2015 at 8:10 AM, Sebastian Bergmann <sebastian@php.net>
wrote:

>  The following is currently valid PHP 7 code
>
>      <?php
>      function a(\int $i) {}
>
>  Is it intentional that the \ in front of the "int" is allowed? IMHO, this
>  confusing notation must not be allowed.
>
>
> Why not? Its a root level type, you can prefix a \ on any other root level
type that's valid for use in type hinting.

function a(\DateTime $d) {}
function b(\SplFileObject $f) {}

Also, this is the only way to get some IDEs to recognize the type when in a
namespace (at least currently).

---

## #89377 — Theodore Brown — 2015-11-24T15:42:09+00:00

URL: https://externals.io/message/89375#89377

> On Tue, Nov 24, 2015 at 8:10 AM, Sebastian Bergmann <sebastian@php.net>
> wrote:
>
> > […]
> type that's valid for use in type hinting.
>
> function a(\DateTime $d) {}
> function b(\SplFileObject $f) {}
>
> Also, this is the only way to get some IDEs to recognize the type when in a
> namespace (at least currently).

The difference is that DateTime and \DateTime mean different things inside a namespace. int and \int always mean the same thing, so it seems confusing to allow the latter syntax as if it means something different.

---

## #89378 — Sebastian Bergmann — 2015-11-24T15:51:16+00:00

URL: https://externals.io/message/89375#89378 · In reply to: #89376

> Also, this is the only way to get some IDEs to recognize the type when in a
> namespace (at least currently).

  Then the IDEs have to be fixed.

---

## #89380 — Sebastian Bergmann — 2015-11-24T15:52:42+00:00

URL: https://externals.io/message/89375#89380 · In reply to: #89377

> The difference is that DateTime and \DateTime mean different things inside a namespace.
> int and \int always mean the same thing, so it seems confusing to allow the latter syntax
> as if it means something different.

  That. And the fact that DateTime, for instance, is a built-in class
  whereas int is a type (I know that classes are types but you know what I
  mean).

---

## #89385 — Andrea Faulds — 2015-11-24T16:47:55+00:00

URL: https://externals.io/message/89375#89385 · In reply to: #89375

Hi Sebastian,

Sebastian Bergmann wrote:
>   The following is currently valid PHP 7 code
>
>       <?php
>       function a(\int $i) {}
>
>   Is it intentional that the \ in front of the "int" is allowed? IMHO, this
>   confusing notation must not be allowed.

This is weird and I'd consider it a bug. You can't do \array or 
\callable, and if I saw \int, I'd think it meant a class of that name 
rather than a scalar type.

Can this be fixed for 7.0.0?

Thanks.

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

---

## #89386 — Stanislav Malyshev — 2015-11-24T16:57:30+00:00

URL: https://externals.io/message/89375#89386 · In reply to: #89385

Hi!

> > […]
> 
> This is weird and I'd consider it a bug. You can't do \array or
> \callable, and if I saw \int, I'd think it meant a class of that name
> rather than a scalar type.

I would assume \int means class named "int", as opposed to "int" type.
But banning it may be ok too.

> 
> Can this be fixed for 7.0.0?

I don't think this would be a good idea. We're in the final stretch of
release cycle, and should not do any non-urgent fixes. This does not
look urgent. It can wait for 7.0.1.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #89387 — Anthony Ferrara — 2015-11-24T17:03:39+00:00

URL: https://externals.io/message/89375#89387 · In reply to: #89375

Sebastian,


>  The following is currently valid PHP 7 code
>
>      <?php
>      function a(\int $i) {}
>
>  Is it intentional that the \ in front of the "int" is allowed? IMHO, this
>  confusing notation must not be allowed.

Yeah, that is a problem. We should fix that prior to gold. I'll take a
peak at it when I get some time, but if someone else has a few minutes
and wants to tackle it go for it.

Anthony

---

## #89388 — Andrea Faulds — 2015-11-24T17:16:22+00:00

URL: https://externals.io/message/89375#89388 · In reply to: #89386

Hi Stas,

Stanislav Malyshev wrote:
> Hi!
>
> > […]
>
> I would assume \int means class named "int", as opposed to "int" type.

That's also what I'd expect. However, "int" is not allowed as a class 
name in PHP 7. And unfortunately what the code Sebastian posted sctually 
does is act as an integer type hint, not as a class type hint.

> > […]
>
> I don't think this would be a good idea. We're in the final stretch of
> release cycle, and should not do any non-urgent fixes. This does not
> look urgent. It can wait for 7.0.1.

It can't wait for 7.0.1, because banning this would be a 
backwards-compatibility break with 7.0.0. We have to fix it in 7.0.0 or 
not fix it ever.

Thanks.

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

---

## #89389 — Stanislav Malyshev — 2015-11-24T17:28:04+00:00

URL: https://externals.io/message/89375#89389 · In reply to: #89388

Hi!

> It can't wait for 7.0.1, because banning this would be a
> backwards-compatibility break with 7.0.0. We have to fix it in 7.0.0 or
> not fix it ever.

In theory, yes. In practice, if somebody starts using 7.0.0 and
immediately jumps to using \int, I don't feel too bad for breaking that
code. We can put a note in release notes for this is needed. But the
risk of changing syntax parts on the brink of GA IMHO is much larger
than the risk of somebody using \int in 7.0.0 and getting breakage in
7.0.1. Especially if it's clearly described as a bug we intend to fix.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #89390 — Chris Riley — 2015-11-24T17:29:22+00:00

URL: https://externals.io/message/89375#89390 · In reply to: #89388

> Hi Stas,
>
> Stanislav Malyshev wrote:
>
> > […]
>
> That's also what I'd expect. However, "int" is not allowed as a class name
> in PHP 7. And unfortunately what the code Sebastian posted sctually does is
> act as an integer type hint, not as a class type hint.
>
> Can this be fixed for 7.0.0?
> > […]
>
> It can't wait for 7.0.1, because banning this would be a
> backwards-compatibility break with 7.0.0. We have to fix it in 7.0.0 or not
> fix it ever.
>
> Thanks.
>
> --
> Andrea Faulds
> http://ajf.me/
>
> --
> 
> 
>
>
Could we not just document it as \int is not allowed and throws a fatal
error. Then we'd just be fixing a bug in documented behaviour.

~C

---

## #89391 — Andrea Faulds — 2015-11-24T17:50:13+00:00

URL: https://externals.io/message/89375#89391 · In reply to: #89389

Hi Stas,

Stanislav Malyshev wrote:
> Hi!
>
> > […]
>
> In theory, yes. In practice, if somebody starts using 7.0.0 and
> immediately jumps to using \int, I don't feel too bad for breaking that
> code. We can put a note in release notes for this is needed. But the
> risk of changing syntax parts on the brink of GA IMHO is much larger
> than the risk of somebody using \int in 7.0.0 and getting breakage in
> 7.0.1. Especially if it's clearly described as a bug we intend to fix.

There's no syntax change. We'd be adding another fatal error to 
zend_compile.c triggered by a flag on the token. No messing around with 
the parser.

I understand your concern about the risk, but it's the kind of change 
that wouldn't break anything without it being tremendously obvious.

Thanks.

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

---

## #89392 — Joshua Holmer — 2015-11-24T18:43:31+00:00

URL: https://externals.io/message/89375#89392 · In reply to: #89389

>
> Hi!
>
> > […]
>
> In theory, yes. In practice, if somebody starts using 7.0.0 and
> immediately jumps to using \int, I don't feel too bad for breaking that
> code. We can put a note in release notes for this is needed. But the
> risk of changing syntax parts on the brink of GA IMHO is much larger
> than the risk of somebody using \int in 7.0.0 and getting breakage in
> 7.0.1. Especially if it's clearly described as a bug we intend to fix.
>

Hi,

Isn't it quite worse to fix this in a patch release than to fix it prior to
GA? If something like this breaks prior to GA, that's tolerated. People
have been warned not to use pre-release versions of PHP7 in production.
Once 7.0.0 is out, people expect stability. If something breaks between
patch releases, even an undocumented behavior like this, it makes PHP look
bad. Even if you call this change a bug fix, there's no reason not to put
it in now, since isn't the purpose of the RCs to fix as many bugs as
possible before GA?

Josh Holmer

---

## #89394 — Matteo Beccati — 2015-11-24T19:30:42+00:00

URL: https://externals.io/message/89375#89394 · In reply to: #89391

> There's no syntax change. We'd be adding another fatal error to
> zend_compile.c triggered by a flag on the token. No messing around with
> the parser.
> 
> I understand your concern about the risk, but it's the kind of change
> that wouldn't break anything without it being tremendously obvious.

I agree and we should be still in time for RC8.


Cheers
-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/

---

## #89395 — Sebastian Bergmann — 2015-11-24T20:39:59+00:00

URL: https://externals.io/message/89375#89395 · In reply to: #89385

> Can this be fixed for 7.0.0?

  I sure hope so.

---

## #89396 — Bob Weinand — 2015-11-24T20:49:02+00:00

URL: https://externals.io/message/89375#89396 · In reply to: #89394

> Am 24.11.2015 um 20:30 schrieb Matteo Beccati <php@beccati.com>:
> 
> 
> > […]
> 
> I agree and we should be still in time for RC8.
> 
> 
> Cheers
> -- 
> Matteo Beccati

Hey,

I fixed the issue via http://git.php.net/?p=php-src.git;a=commitdiff;h=569763cb1ac67f56e7743062ca8b3b7c650c1254

I think too this should go into PHP 7.0.0 as it is some type of a language related change (even if not directly failing in parser...)

Bob

---

## #89398 — Xinchen Hui — 2015-11-25T03:47:23+00:00

URL: https://externals.io/message/89375#89398 · In reply to: #89396

Hey:





> > […]
>
> Hey,
>
> I fixed the issue via
> http://git.php.net/?p=php-src.git;a=commitdiff;h=569763cb1ac67f56e7743062ca8b3b7c650c1254
>
> I think too this should go into PHP 7.0.0 as it is some type of a language
> related change (even if not directly failing in parser...)
>
>
I've made a improvement to the fix(
https://github.com/php/php-src/commit/00865ae22f2c5fdee9e500ce79d442467e0a0899)
,

before this, \array will result a syntax , but \int result a compiler
error, which seems a little in-consistent.

thanks

> Bob
> --
> 
> 
>



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #89400 — Nikita Popov — 2015-11-25T09:57:17+00:00

URL: https://externals.io/message/89375#89400 · In reply to: #89398

> Hey:
>
>
>
> 
>
> > […]
> with
> > […]
> http://git.php.net/?p=php-src.git;a=commitdiff;h=569763cb1ac67f56e7743062ca8b3b7c650c1254
> > […]
> language
> > […]
> I've made a improvement to the fix(
>
> https://github.com/php/php-src/commit/00865ae22f2c5fdee9e500ce79d442467e0a0899
> )
> ,
>
> before this, \array will result a syntax , but \int result a compiler
> error, which seems a little in-consistent.
>

Imho this additional change is not necessary, it only makes the parser more
complicated.

However something missing from the original patch is handling of relative
names like namespace\int. Instead of checking for ast->attr == ZEND_NAME_FQ
it should check for ast->attr != ZEND_NAME_NOT_FQ.

Nikita

---

## #89401 — Anatol Belski — 2015-11-25T10:11:00+00:00

URL: https://externals.io/message/89375#89401 · In reply to: #89389

Hi Stas,

> -----Original Message-----
> From: Stanislav Malyshev [mailto:smalyshev@gmail.com]
> Sent: Tuesday, November 24, 2015 6:28 PM
> To: Andrea Faulds <ajf@ajf.me>; internals@lists.php.net
> Subject: Re: [PHP-DEV] Re: Scalar Type Declaration Syntax Weirdness
> 
> Hi!
> 
> > […]
> 
> In theory, yes. In practice, if somebody starts using 7.0.0 and immediately jumps
> to using \int, I don't feel too bad for breaking that code. We can put a note in
> release notes for this is needed. But the risk of changing syntax parts on the
> brink of GA IMHO is much larger than the risk of somebody using \int in 7.0.0
> and getting breakage in 7.0.1. Especially if it's clearly described as a bug we
> intend to fix.
> 
that's my point as well. There is a clear documentation about type hints, usage of an undocumented way is out of scope of BC. Using \int means there were a "class int{}" which is prohibited. Of course it is a bug after all, which will be addressed. 	

Regards

Anatol

---

## #89404 — Xinchen Hui — 2015-11-25T11:06:27+00:00

URL: https://externals.io/message/89375#89404 · In reply to: #89400

Hey:




> 
>
> > […]
>
> Imho this additional change is not necessary, it only makes the parser
> more complicated.
>
> However something missing from the original patch is handling of relative
> names like namespace\int. Instead of checking for ast->attr == ZEND_NAME_FQ
> it should check for ast->attr != ZEND_NAME_NOT_FQ.
>
> yeah, you are right,  namespace\array still behavior difference from
namespace\int.

I am going to revert my part.

thanks

> Nikita
>



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #89405 — Xinchen Hui — 2015-11-25T11:10:12+00:00

URL: https://externals.io/message/89375#89405 · In reply to: #89400

Hey:



> 
>
> > […]
>
> Imho this additional change is not necessary, it only makes the parser
> more complicated.
>
> However something missing from the original patch is handling of relative
> names like namespace\int. Instead of checking for ast->attr == ZEND_NAME_FQ
> it should check for ast->attr != ZEND_NAME_NOT_FQ.
>

PS: However, namespace\int will result error while checking valid
classname, as int is reserved keywords.

so I think check for == ZEND_NAME_FQ is enough here.

thanks

>
>

>
> Nikita
>



-- 
Xinchen Hui
@Laruence
http://www.laruence.com/

---

## #89431 — Andrea Faulds — 2015-11-25T19:58:20+00:00

URL: https://externals.io/message/89375#89431 · In reply to: #89405

Hi,

Xinchen Hui wrote:
> Hey:
>
> 
>
> > […]
>
> PS: However, namespace\int will result error while checking valid
> classname, as int is reserved keywords.
>
> so I think check for == ZEND_NAME_FQ is enough here.
>

This is how I feel as well. You can't make a class with that name anyway 
(at least for now), so we don't need to prohibit it. \int was a problem 
because it was interpreted the same as 'int'.

Thanks.

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

---

## #89432 — Andrea Faulds — 2015-11-25T20:00:12+00:00

URL: https://externals.io/message/89375#89432 · In reply to: #89401

Hi,

Anatol Belski wrote:
> that's my point as well. There is a clear documentation about type hints, usage of an undocumented way is out of scope of BC. Using \int means there were a "class int{}" which is prohibited. Of course it is a bug after all, which will be addressed. 	

It may not be documented, but that doesn't put it outside the scope of 
BC. People will unintentionally rely on bugs.

Thanks.

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

---

## #89436 — Anatol Belski — 2015-11-25T23:28:56+00:00

URL: https://externals.io/message/89375#89436 · In reply to: #89432

Hi Andrea,

> -----Original Message-----
> From: Andrea Faulds [mailto:ajf@ajf.me]
> Sent: Wednesday, November 25, 2015 9:00 PM
> To: internals@lists.php.net
> Subject: Re: [PHP-DEV] Re: Scalar Type Declaration Syntax Weirdness
> 
> Hi,
> 
> Anatol Belski wrote:
> > […]
> of an undocumented way is out of scope of BC. Using \int means there were a
> "class int{}" which is prohibited. Of course it is a bug after all, which will be
> addressed.
> 
> It may not be documented, but that doesn't put it outside the scope of BC.
> People will unintentionally rely on bugs.
> 
What is the reason to use \int if "class \int{}" is prohibited? A typo :) ? 

If the patch is fine within RC8 period, it is going to be applied in 7.0.1 which is soon enough to avoid creating bad habits. Applying a couple of hours old patch to the bug with such severity seems not appropriate, anyway.

Regards

Anatol

---

## #89437 — Andrea Faulds — 2015-11-25T23:46:54+00:00

URL: https://externals.io/message/89375#89437 · In reply to: #89436

Hi Anatol,

Anatol Belski wrote:
> > […]
> What is the reason to use \int if "class \int{}" is prohibited? A typo :) ?

It might be used deliberately since some IDEs (PHPStorm in particular) 
can't handle PHP 7's scalar type hints properly in namespaces yet.

Thanks.

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

---

Next page: https://externals.io/message/89375.md?page=2
