php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66566 Parse error when files contain shebang and NS declaration
Submitted: 2014-01-24 02:23 UTC Modified: 2019-07-15 14:30 UTC
Votes:22
Avg. Score:3.7 ± 0.7
Reproduced:18 of 18 (100.0%)
Same Version:8 (44.4%)
Same OS:9 (50.0%)
From: eric at wepay dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2014-01-24 02:23 UTC] eric at wepay dot com
Description:
------------
If a PHP CLI scripts starts with a shebang and contains a namespace declaration, executing it via stdin incorrectly raises a fatal error stating the NS declaration must be the first thing in the file. This is a relatively common thing to do when, for example, trying to lint a file in a commit hook (git show ... | php -l)

Test script:
---------------
eric@Eric-MBP ~: cat testcase.php 
#!/usr/bin/env php
<?php

namespace Boris;

eric@Eric-MBP ~: php -l testcase.php 
No syntax errors detected in testcase.php

eric@Eric-MBP ~: cat testcase.php | php -l

Fatal error: Namespace declaration statement has to be the very first statement in the script in - on line 4

Errors parsing -


Expected result:
----------------
No error, return code 0. This is what happens if the file does not contain the leading shebang.

Actual result:
--------------
Fatal error:  Namespace declaration statement has to be the very first statement in the script

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-24 03:35 UTC] willfitch@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: willfitch
 [2014-01-24 03:54 UTC] willfitch@php.net
-PHP Version: 5.5.8 +PHP Version: 5.3
 [2014-01-24 03:54 UTC] willfitch@php.net
Verified from PHP 5.3+
 [2016-06-28 11:07 UTC] yunosh@php.net
Fixed in PHP 7
 [2016-11-29 19:09 UTC] miken32 at gmail dot com
No, this remains an issue in version 7.0.13.
 [2017-10-24 03:28 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: willfitch +Assigned To:
 [2017-10-24 03:28 UTC] kalle@php.net
-Operating System: OS X, CentOS +Operating System: * -PHP Version: 5.3 +PHP Version: *
 [2018-03-31 17:34 UTC] cmanley at xs4all dot nl
Still an issue in PHP 7.0.27-0+deb9u1 too.
 [2019-07-15 14:30 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 08:01:24 2019 UTC