Connecting Tech Pros Worldwide Help | Site Map

Returning error message from Classes

  #1  
Old November 14th, 2008, 04:45 AM
FutureShock
Guest
 
Posts: n/a
I am using a registration class to process a registration form and need
some opinions on returning error messages.

I am self referring the page on submit.

I would like to send each form field to the class for processing
(validating, sanitizing, etc..) So far no problem.

Now I am throwing ideas around on how best to check for error messages
to the users.

One idea was to expect and array from the class as a return value, set
the error key in the function if needed and check if the error key is
set for each call.

public function validateEmail($post) {
//if no error after validating
$result['result'] = $post;
return $result
//if error
$result['error'] = 'Please input a valid email';
return $result;
}


$result = $register->validateEmail($_POST['email']);
if(isset($result['error'])){
//process for display
} elseif(isset($result['result'])) {
//process for DB
}

************************************************** ***************

Second idea was to use a PUBLIC variable in the class and test it.

public $error;

public function validateEmail($post) {
//if no error after validating
$result = $post
return $result;

//if error
$this->$error['email'] = "Please enter a valid email";
}

$result = $register->validateEmail($_POST['email']);
if(isset($error['email]){
//process for display
} else {
//process $result
}

************************************************** ****************

I have only tested the first idea and it has worked so far.

I am still learning OOP so the second idea may not even be good practice
or might not even work as predicted.

I know some of you out there have come across this and am curious what
has worked for you.

Thanks
Scotty
  #2  
Old November 14th, 2008, 11:45 AM
Jerry Stuckle
Guest
 
Posts: n/a

re: Returning error message from Classes


FutureShock wrote:
Quote:
I am using a registration class to process a registration form and need
some opinions on returning error messages.
>
I am self referring the page on submit.
>
I would like to send each form field to the class for processing
(validating, sanitizing, etc..) So far no problem.
>
Now I am throwing ideas around on how best to check for error messages
to the users.
>
One idea was to expect and array from the class as a return value, set
the error key in the function if needed and check if the error key is
set for each call.
>
public function validateEmail($post) {
//if no error after validating
$result['result'] = $post;
return $result
//if error
$result['error'] = 'Please input a valid email';
return $result;
}
>
>
$result = $register->validateEmail($_POST['email']);
if(isset($result['error'])){
//process for display
} elseif(isset($result['result'])) {
//process for DB
}
>
That's one possibility.
Quote:
************************************************** ***************
>
Second idea was to use a PUBLIC variable in the class and test it.
>
public $error;
>
public function validateEmail($post) {
//if no error after validating
$result = $post
return $result;
>
//if error
$this->$error['email'] = "Please enter a valid email";
}
>
$result = $register->validateEmail($_POST['email']);
if(isset($error['email]){
//process for display
} else {
//process $result
}
>
Good OO design dictates you (almost) never have public variables. If
you want to do it this way, you should have the variable private and
have a get method to access it (maybe also a reset_errors method to
clear it, although that's generally not needed).
Quote:
************************************************** ****************
>
I have only tested the first idea and it has worked so far.
>
I am still learning OOP so the second idea may not even be good practice
or might not even work as predicted.
>
I know some of you out there have come across this and am curious what
has worked for you.
>
Thanks
Scotty
There isn't a "right" way - there is nothing wrong (other than the
public variable) with either case.

One way to think of it is that (among other things), OO is supposed to
limit code duplication. So if you use the same class in 2 (or 20 or
200) pages, are you going to always want the same error messages? If
so, then I would recommend keeping the messages in the class itself.
But if you're going to want different messages, then you would want to
keep them separate.

A third way would be to have a message class with all the messages your
class(es) use. This makes multilingual sites much easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #3  
Old November 14th, 2008, 02:25 PM
FutureShock
Guest
 
Posts: n/a

re: Returning error message from Classes


Jerry Stuckle wrote:
Quote:
FutureShock wrote:
Quote:
>I am using a registration class to process a registration form and
>need some opinions on returning error messages.
>>
>I am self referring the page on submit.
>>
>I would like to send each form field to the class for processing
>(validating, sanitizing, etc..) So far no problem.
>>
>Now I am throwing ideas around on how best to check for error messages
>to the users.
>>
>One idea was to expect and array from the class as a return value, set
>the error key in the function if needed and check if the error key is
>set for each call.
>>
>public function validateEmail($post) {
> //if no error after validating
> $result['result'] = $post;
> return $result
> //if error
> $result['error'] = 'Please input a valid email';
> return $result;
>}
>>
>>
>$result = $register->validateEmail($_POST['email']);
>if(isset($result['error'])){
> //process for display
>} elseif(isset($result['result'])) {
> //process for DB
>}
>>
>
That's one possibility.
>
Quote:
>************************************************* ****************
>>
>Second idea was to use a PUBLIC variable in the class and test it.
>>
>public $error;
>>
>public function validateEmail($post) {
> //if no error after validating
> $result = $post
> return $result;
>>
> //if error
> $this->$error['email'] = "Please enter a valid email";
>}
>>
>$result = $register->validateEmail($_POST['email']);
>if(isset($error['email]){
> //process for display
>} else {
> //process $result
>}
>>
>
Good OO design dictates you (almost) never have public variables. If
you want to do it this way, you should have the variable private and
have a get method to access it (maybe also a reset_errors method to
clear it, although that's generally not needed).
>
Quote:
>************************************************* *****************
>>
>I have only tested the first idea and it has worked so far.
>>
>I am still learning OOP so the second idea may not even be good
>practice or might not even work as predicted.
>>
>I know some of you out there have come across this and am curious what
>has worked for you.
>>
>Thanks
>Scotty
>
There isn't a "right" way - there is nothing wrong (other than the
public variable) with either case.
>
One way to think of it is that (among other things), OO is supposed to
limit code duplication. So if you use the same class in 2 (or 20 or
200) pages, are you going to always want the same error messages? If
so, then I would recommend keeping the messages in the class itself. But
if you're going to want different messages, then you would want to keep
them separate.
>
A third way would be to have a message class with all the messages your
class(es) use. This makes multilingual sites much easier.
>
OK thanks for the input and insight.
I will definitely need to implement the message class for varied output.

Using the PUBLIC variable kind of set uneasy with me after most of the
stuff I have read and since I was sitting on the fence with that, your
input helps sway me off of it towards the array option.

Thanks
Scotty
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning an error message when something goes wrong Simon answers 7 November 16th, 2005 04:20 PM
Error Trapping In Access 2000 Peter Frost answers 6 November 12th, 2005 11:14 PM
Error Trapping In Access 2000 Peter Frost answers 6 November 12th, 2005 10:50 PM
PHPMailer: HT avoid error msges going back to wwwrun? Daniel Loose answers 1 July 17th, 2005 02:49 PM