0

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.

4

3 回答 3

1

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:

$config['environment'] = 'production';

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:

if($config['environment'] == 'production') {
    echo 'Oops, something went wrong';
} else {
    // print full backtrace and stuff
}

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.

于 2012-07-15T07:22:04.023 回答
0

You can build a custom exception class that handles exceptions in a user-friendly way.

class MyCoolException extends Exception
{
  //Throw exceptions in a friendly way.
  //Perhaps redirect them to a custom error page?

  public function __construct( $error )
  {
    //do whatever with your error data
  }

  public function redirect()
  {
  }
}

Then, setup your try/catch pattern to accept it:

try
{
}
catch( MyCoolException $e )
{
}
catch( Exception $e )
{
}

As another answer says, you can direct the flow of throwing exceptions based on environment configuration.

于 2012-07-15T07:37:45.570 回答
0

There is a much simpler way using PHP. Use set_exception_handler() http://php.net/manual/en/function.set-exception-handler.php.

What this function does is, whenever an exception is thrown, it calls a particular function always. You can use this function to check the current environment and act accordingly.

But you will have to tell your script which environment it is in. For that, I agree with what ChocoDeveloper has said in his answer.

For example:

<?php
function custom_exception_handler_function($exception) {
  global $config;
  if ($config['environment'] == 'production') {
    continue;
  } else {
    throw $exception;
  }
}

set_exception_handler('custom_exception_handler_function');
?>

This way all your uncaught exceptions (i.e. those which are not in try-catch block) will be tackled uniformly.

于 2012-07-15T07:51:10.070 回答