I have a PHP script that will throw exceptions (e.g. cannot connect to server). In the development arena I would like as much information to be on the screen, but in production I do not want to give the game away. But I would like some code that works in both environments and also enables me to further develop the code.
Is there a prescribed technique to achieve this?
EDIT
Yes using apache with PHP
But how to distinguish between the production environment and the development environment without having to change the code base.
You usually deal with this with environment variables. It can be a configuration file .php (or .ini, .yml, .xml, whatever you use for config files) that you load on startup, and has environment-specific values. For example, in your production server, your environment config file might look like this:
Another option is to use environment variables provided by your web server. Both approaches are fine, though I prefer the former.
Then you catch ALL uncaught exceptions (and also turn errors into exceptions. Having errors disabled can be annoying during development, and having errors enabled and not catching them can be a huge security hole in production) with an ‘universal error catcher’ (I haven’t tried this one, but looks good. In the past I used one I wrote myself, but it was very tricky, I don’t recommend writing your own catcher, other than for learning purposes). Then just do something like this:
As a sidenote, this is the kind of problem a ‘framework’ is supposed to solve for you. If you use Silex or Symfony 2, this functionality is available by default.