tags:

views:

201

answers:

2

What's the best practice to handle errors if using objects?

A) Before the method of object is called and not even getting to execute method if there's some error, or

B) Just pass parameters and perform error checking in method itself, returning error code or something.

Please pick your option and short description, why?

Thanks orlandu63, it is good practice, but what about non-fatal errors, such as user should provide a title for something, and he/she didn't?

class Sample {    

var $err_no_title = 1;

function createNewRecord ($title) {
       if (!$title) return $this->err_no_title;
    }
}

Or use exceptions for these kind of errors also?

+3  A: 

If you're using OO, you might as well use Exceptions. My answer is a mix of both A and B:

class DatabaseConnectionException extends Exception {}

class Database {
    public function connect($user, $pass, $db) {
        //Connection stuff.
        if($baduser) {
            throw new DatabaseConnectionException('Username (' . $user. ') is invalid.')
         }
         if($badpass) {
             //''
         }
    }
}

$db = new Database;
try {
    $db->connect($user, $pass, $db);
catch (DatabaseConnectionException $e) {
    die('I cannot connect to the database:' . $e);
}

What are the advantages to this? I don't know, but it seems right.

You can read more on it on http://php.net/exceptions and google.

Regarding your second part,

First of all your example will treat it more of an error than a "warning" since you exit the function and thus don't create a record if you have no title. This shows that method B is flawed. So method A all the way.

orlandu63
You don't even need to be using pure OO to benefit from Exceptions in PHP.
Toby Hede
A: 

To choose from options you offer it would be B, but don't use error codes and throw exceptions instead. All the logic (even validation of inputs) should be encapsulated in the function.

The reasons are:

  1. The function may change and so may change requirements for inputs.
  2. User of your function may not always know, what the inputs should be like.
  3. You surely don't want to repeat the validation code everywhere you use the function.

Be careful though, as exceptions are raised by object oriented code only. For example this code does not fire an exception:

<?php  
  $number = $number / 0;
?>

Your example would be like:

<?php
    class Sample {    
      function createNewRecord ($title) {
       if (!$title) throw new Exception('Title required');
      }
    }

    ...
    try {
      $mysample->createNewRecord($title);
    } catch ($ex) {
      echo "Could not create record. Please try again. (Reason: $ex)";
    }
    ...
?>

In the first place, this sort of thing should be validated in the user interface. So it would be A, but B needs to be there to. So final verdict would be: both.

Josef Sábl
But then it's not a "warning" anymore, it's an error. You won't be able to create a new record without a title, which is what the question-asker wants.
orlandu63