Migrating Legacy PHP application to Symfony 5, strange issue with globals

I have a strange issue with, I'm guessing, php-fpm configuration.

I'm migrating a legacy application in to Symfony 5. So far it's gone pretty well with a LegacyBridge and everything works fine locally in a docker container and on a newly installed "staging" server, a KVM virtual machine running CentOS 8.

Now that everything is working fine in development the next step is to go live. I've set up a Standard Linode with CentOS 8 configured as close to my staging server as I've been able to copy. MariaDB, nginx and php-fpm 7.4.5 from remi:php-7.4.

Symfony is loaded via index_symfony.php which loads the legacy code via index.php. In index.php I have the following code:

13 global $kernel;
14 $pdo = $kernel->getContainer()
      ->get('doctrine.orm.default_entity_manager')
      ->getConnection()
      ->getWrappedConnection();

On my local setup and on the staging server this works just fine but on the Linode I get this:

PHP Fatal error:  Uncaught Error: Call to a member function getContainer() on null in /var/www/.../public/index.php on line 14

If I on the other hand copy that same code to index_symfony.php (after kernel instantiation, adding global $pdo) those lines work just fine but both the $pdo and $kernel objects seem to disappear before index.php is loaded.

I've tried messing around with the PHP code to work around this issue but now I'm pretty sure that there's some configuration or possibly extension that I'm missing.

Anyone have an idea?

2 Replies

Unless $kernel and $pdo are declared inside a function/method, they probably shouldn't be declared global…they are de-facto global.

If you add

"filp/whoops": "^1.1"

to your composer.json and do a composer update and then add

$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();

to your index.php at the very top right after

require_once "autoload.php";

you will get a nice tombstone with a backtrace about your failure. Note that $whoops in the above snippet would be global.

-- sw

I've solved it but I'm not 100% certain how. There was a bug in another file that I'm guessing made the index.php file think it was in a different scope so it couldn't find the global variables. Why that didn't trigger on the staging server I will probably never find out.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct