Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\Response (or ResponseHeaderBag) #411

Closed
ieleja opened this issue Dec 16, 2016 · 19 comments

Comments

@ieleja
Copy link

ieleja commented Dec 16, 2016

OSX, Apache 2.4, PHP 5.6.27
Linux, Apache 2.4, PHP 5.6.28

clean Symfony Demo install with:
$ symfony demo

point browser to:
http://localhost/symfony_demo/web/en/blog/

choose:
Browse Application
Back to blog

and always get this Fatal error under OSX:
PHP Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\ResponseHeaderBag in /Users/User/Sites/symfony_demo/var/cache/prod/classes.php on line 4491
or
PHP Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\Response in /Users/User/Sites/symfony_demo/var/cache/prod/classes.php on line 3777
and under Linux:
PHP Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\ResponseHeaderBag in /var/www/symfony_demo/var/cache/prod/classes.php on line 4491

clearing cache helps exactly one time, up to server recreates cache files

error reproduces in Chrome, Firefox, Safari

@bocharsky-bw
Copy link
Contributor

What version of symfony/symfony do you use?

@voronkovich
Copy link
Contributor

@ieleja, do you clean the cache manually, without warmup? Try to use php bin/console cache:clear --env=prod --no-debug.

@ieleja
Copy link
Author

ieleja commented Dec 17, 2016

bash-3.2$ bin/console --version
Symfony 3.2.0 (kernel: app, env: dev, debug: true)

==============

bash-3.2$ php bin/console cache:clear --env=prod --no-debug

// Clearing the cache for the prod environment with debug false

[OK] Cache for the "prod" environment (debug=false) was successfully cleared.

==============

after few successful clicks on Login/Logout/Homepage I get again:

PHP Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\Response in /Users/User/Sites/symfony_demo/var/cache/prod/classes.php on line 3777
and
HP Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\ResponseHeaderBag in /var/www/symfony_demo/var/cache/prod/classes.php on line 4491

p.s.
macOS, Debian Linux, Chrome, Firefox is up-to-date

@RenWasHere
Copy link

RenWasHere commented Dec 28, 2016

Same issue in Symfony 3.2.1.

When viewing: /en/blog/

Fatal error: Cannot declare class Symfony\Component\HttpFoundation\ResponseHeaderBag, because the name is already in use in \var\cache\prod\classes.php on line 4491

Clearing the cache does fix it until you load another page.

PHP 7.0.10
Apache 2.4.23
MySQL 5.7.14
Windows 10 Pro

@bocharsky-bw
Copy link
Contributor

I'm getting the same issue on PHP 7.0 and 7.1

@javiereguiluz
Copy link
Member

javiereguiluz commented Dec 28, 2016

I can't reproduce with latest Symfony Demo + Symfony 3.2.1 + PHP 7.1 + macOS.

Please, tell me the exact steps to reproduce it:

  1. $ symfony demo
  2. $ cd symfony_demo
  3. php bin/console server:start
  4. Browse 127.0.0.1:8000
  5. ...

@bocharsky-bw
Copy link
Contributor

bocharsky-bw commented Dec 28, 2016

@javiereguiluz It occurs for me only with the prod environment. My steps to reproduce are:

  1. Clone repo (the latest master commit)
  2. composer ins
  3. bin/console s:r
  4. rm -rf var/cache/*
  5. Browser http://127.0.0.1:8000/app.php/
  6. Press "Browser application"
  7. Press "Symfony Demo"
  8. Press "Browser application" again
  9. If you don't see "Fatal error..." - repeat 6-8 steps

Anyway, for me, this error happens only with prod env on blog list page, i.e. when surfing Symfony Demo app and return to the http://127.0.0.1:8000/app.php/ru/blog/ page

@javiereguiluz
Copy link
Member

@bocharsky-bw thanks! I could reproduce it now. They key for me was: use the prod environment and repeat the navigation several times until this error appears:

symfony-demo-error

@bocharsky-bw
Copy link
Contributor

Yeah, it happens randomly... sometimes I need to repeat the navigation several times too.

javiereguiluz added a commit that referenced this issue Jan 2, 2017
…on PHP 7 (bocharsky-bw)

This PR was merged into the master branch.

Discussion
----------

Fix error with class redeclaration: Do not load cache files on PHP 7

Fix #411

I think this error could be related to the symfony/symfony-standard#1030

Commits
-------

13da5dc Fix error with class redeclaration: Do not load cache files on PHP 7
@maximivanov
Copy link

Doesn't seem to be PHP 7 specific issue. I can reproduce the problem following steps by bocharsky-bw with PHP 5.6.23, OS X, Symfony 3.2.1

@javiereguiluz
Copy link
Member

javiereguiluz commented Jan 6, 2017

Reopening and kindly asking @nicolas-grekas to take a look at this because this seems like a very tricky bug. Thanks!

@javiereguiluz javiereguiluz reopened this Jan 6, 2017
@bocharsky-bw
Copy link
Contributor

Hm, seems it's due to this @Cache line I think: https://github.com/symfony/symfony-demo/blob/master/src/AppBundle/Controller/BlogController.php#L40
Probably this bug relates to the Symfony in general, not only to the Demo app.

fabpot added a commit to symfony/symfony that referenced this issue Jan 6, 2017
… (nicolas-grekas)

This PR was merged into the 3.2 branch.

Discussion
----------

[FrameworkBundle] Remove Response* from classes to compile

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20560, symfony/demo#411, FriendsOfSymfony/FOSHttpCacheBundle#276
| License       | MIT
| Doc PR        | -

When HttpCache is used, Response is loaded first, then the kernel is booted (on cache miss), which triggers the loading of classes.php. Since 3.2 generates a context free classes.php, the Response class is now included there when it was excluded previously. And boom, "Cannot declare class Symfony\Component\HttpFoundation\Response".

Commits
-------

9ab5982 [FrameworkBundle] Remove Response* from classes to compile
@fabpot fabpot closed this as completed Jan 6, 2017
@ciscolybon
Copy link

[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot declare class AddCategoryIdImageToPosts, because the name is already in use

@xeux
Copy link

xeux commented Dec 12, 2017

This also happens with ConfigCacheInterface on classes.php

@javiereguiluz
Copy link
Member

@xexu which PHP version and Symfony Demo version are you using?

@spacetraveller
Copy link

spacetraveller commented Jan 5, 2018

I get this all the time now, except with a different class name. Note that Dev does not exhibit the issue.

PHP Fatal error: Cannot declare class appProdProjectContainer, because the name is already in use in /var/www/html/myproject/var/cache/prod/appProdProjectContainer.php on line 6930

I have to manually delete the cache folder in order to clear the cache.

Symfony version 3.4

PHP 7.1

@stof
Copy link
Member

stof commented Jan 8, 2018

please open a new issue instead of commenting on an old closed issue (from before the 3.4 release).

@MusawerAli
Copy link

this error occur due to the twice of this code on the top
use Symfony\Component\HttpFoundation\Request;

Remove one use\Request;

@geetesh07
Copy link

png
I am getting this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests