473,748 Members | 10,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($resul t['error'])){
//process for display
} elseif(isset($r esult['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 1872
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($resul t['error'])){
//process for display
} elseif(isset($r esult['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*******@attgl obal.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($resu lt['error'])){
//process for display
} elseif(isset($r esult['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($erro r['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
3102
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 function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
25
2939
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 construction and then.. // some other processing and/or changing 'retval' return retval; }
6
1293
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 Function MessageHandler() as Type Return GetType(EventMessageHandler) End FUnction End Class
1
7274
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 statusCode;
5
10377
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
19599
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
15
13512
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 http://msdn2.microsoft.com/en-us/library/ms996381.aspx Formerly, with classic Microsoft DNA architecture, the ADO Recordset was a primary transport medium, recommended for transmitting data between application tiers. In fact, there are whole books written on the subject.
3
1751
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 the corresponding integer array based on its index into vlist (Element( index )) . MyClass contains an instance of VList, and then tries to call Element(1) and get a pointer to the appropriate integer list (v_1). But what comes out the debugger...
7
2872
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 returned string object still valid after myfunction() has returned? I
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.