473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Returning error message from Classes

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
Nov 14 '08 #1
2 1856
FutureShock wrote:
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.
************************************************** ***************

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).
************************************************** ****************

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.
js*******@attglobal.net
==================
Nov 14 '08 #2
Jerry Stuckle wrote:
FutureShock wrote:
>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.
>************************************************* ****************

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).
>************************************************* *****************

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
Nov 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
6
by: Ken Foster | last post by:
Is it possible to return a class (not an instance of a class) from a function? For example (actually tried this code, it didn't work, but it's what I want sort of): Class EventMessage Public...
1
by: Knecke | last post by:
Hi all. I have a problem with returning a custom Result object with webservice. The classes i use is described below (some fields and properties is removed) public class Result { int...
5
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
3
by: Peter Oliphant | last post by:
Below are the definitions of two classes. VList creates two static integer arrays (v_0, v_1), creates an array of pointers to these arrays (vlist), and has a public method to return a pointer to...
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.