3.3. Code Examples
In the original code base of the project, an object exists that handles all of the error messages and codes called errorObject. The original programmers didn't think their code would ever generate any errors, so they designed the system to output the errorObject's error information directly to the console.
In this example, a 404:Not Found error is being generated. You are going to assume that the error message content and code may change, but the text will always stay in the same format.
class errorObject
{
private $__error;
public function __construct($error)
{
$this->__error = $error;
}
public function getError()
{
return $this->__error;
}
}
class logToConsole
{
private $__errorObject;
public function __construct($errorObject)
{
$this->__errorObject = $errorObject;
}
public function write()
{
fwrite(STDERR,$this->__errorObject->getError());
}
}
/** create the new 404 error object **/
$error = new errorObject("404:Not Found");
/** write the error to the console **/
$log = new logToConsole($error);
$log->write();
In this scenario, a new network admin has been brought into the project. Best practice suggests that a network log for monitoring software should be installed. The package the admin chose requires the errors to be logged to a multicolumn CSV file. The CSV format calls for the first column to be the numeric error code. The second column should be the error text.
This new software package is familiar with the errorObject class. The vendor has provided ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access