I am trying to return exceptions and also formatting them, but I cannot seem to figure it out. What I want is when there is an error (Exception) to return it so I can display the error message. But I want different formats for an error and a success on form submit. As a simple test I am trying the following, but the
return $e->getMessage();
does not work. When I use
echo $e->getMessage();
it does work, but I want my error messages to be displayed in red, and when there are no errors, I want a "success" message in green, to keep it simple. But I do not want to format any HTML or CSS in my PHP class, obviously. I've checked google and searched on stackoverflow loads, but I cannot seem to figure it out, and is must be so simple.
Index.php
<?php
require 'myClass.php';
$myTest = new myClass();
if (isset($_POST["submit"]))
{
$myTest->myCheck();
}
?>
myClass.php:
public function myCheck()
{
try
{
if (empty($this->myField)) {
throw new Exception("Oops! Something went wrong. One or more of the fields were left empty");
}
return true;
}
catch (Exception $e)
{
return $e->getMessage();
}
}
}
?>