Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Fatal error: Cannot declare class" after upgrading to PHP 7.0.8

I was using PHP 7.0.4 and everything was running smoothly and I just upgraded to PHP 7.0.8 and I started getting errors like this one all over the place.

Fatal error: Cannot declare class Plugins\Users\Plugin because the name is already in use in /var/www/html/plugins/Users/Plugin.php on line 8

Does anyone have any idea whats going on, I've been reading through the change logs but that's a patch update it should not break anything?

Additionally, if no solution is found to this problem, how can I downgrade back to 7.0.4? (I'm using ubuntu 16.04 and I just upgraded to the point release)

To sum the comments so far:

  • I have tried disabling opcache this did not solve the problem
  • I am using composer's autoloader - I am not using require or include anywhere
  • This error is happening for a ton of classes, so it is clearly not a code issue (on my part), furthermore the code was working before I upgraded to 7.0.8
like image 343
php_nub_qq Avatar asked Jul 28 '16 01:07

php_nub_qq


2 Answers

There is not enough data to say definitively, however I suspect opcache. That error, in fact, originates in opcache so I suspect until this point the error had been hidden by one of several opcode bugs, probably #66773.

You legitimately have an autoloading issue that needs to be fixed. Rename the class, check your namespaces, and remove any hard requires.

You might be able to restore the former buggy behavior by disabling/re-enabling opcache or reverting to 7.0.4, but really the bug was only masked by a particular combination of autoloading and opcache. It was, indeed, only by chance that the issue went undetected until now and it'll be only by chance that you can mask it again.

So the best course is to fix the issue.

like image 120
bishop Avatar answered Nov 17 '22 16:11

bishop


Well, apparently I was so blinded by the fact that the error started occurring after the upgrade, that I ignored it even though it was right before my eyes.

These are the first few lines of the class in question (and apparently a bunch of other classes):

<?php

namespace Plugins\Users;

use FW\Utility\Models\Plugins\Plugin;

class Plugin extends Plugin {

As you, and finally I, can clearly see, the class name Plugin is ambiguous, but wasn't in 7.0.4, which is weird and is probably caused by the bug described in bishop's answer

Guess I should have listened to Machavity, huh.

like image 1
php_nub_qq Avatar answered Nov 17 '22 15:11

php_nub_qq