473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error handling

Hi,

Is there anyway to set a custom error handler which is actually a
method of a class? i.e. setting the method eh() of class foo as error
handler in the snippet below.

class foo {
function eh()
{ do something }
}
}

Further more, how can I route E_ERROR & E_PARSE errors to my custom
handler? I've tried 'error_reporting(E_ALL)' but non avail.

TIA
Jul 16 '05 #1
12 3530
Xeon wrote:
Hi,

Is there anyway to set a custom error handler which is actually a
method of a class? i.e. setting the method eh() of class foo as error
handler in the snippet below.

class foo {
function eh()
{ do something }
}
}

Further more, how can I route E_ERROR & E_PARSE errors to my custom
handler? I've tried 'error_reporting(E_ALL)' but non avail.

TIA


This is very abstract, and I currently don't have the resources to test
it, but here's an idea:

1. Open up two named pipes, one for stdout and one for stderr
2. Redirect stdout and stderr into the appropriate named pipes
3. In eh(), open a file handle to the named pipes
4. Handle said errors.

This is completely untested. I have no idea what will actually happen here.

Jul 16 '05 #2
In article <78*************************@posting.google.com> ,
xd*****@zworg.com (Xeon) wrote:
Is there anyway to set a custom error handler which is actually a
method of a class? i.e. setting the method eh() of class foo as error
handler in the snippet below.

class foo {
function eh()
{ do something }
}
}

Further more, how can I route E_ERROR & E_PARSE errors to my custom
handler? I've tried 'error_reporting(E_ALL)' but non avail.


Have you read the manual's chapter on error handling <http://php.net/errorfunc>? You may find it helpful to look at the PEAR_error routines for examples
of what's possible. <http://pear.php.net/package-info.php?package=PEAR>

--
CC
Jul 16 '05 #3
Why on earth does your error handler have to be a method in a class?
Error handling is a single stand-alone process so creating a class
with a single method is total overkill. You can achieve all the error
hanfling you want in a standard function. Take a look at
http://www.tonymarston.net/php-mysql/errorhandler.html for a working
example.

Programmers who insist that everything must be a class drive me nuts.
It is not always the best method.
Tony Marston
http://www.tonymarston.net/
xd*****@zworg.com (Xeon) wrote in message news:<78*************************@posting.google.c om>...
Hi,

Is there anyway to set a custom error handler which is actually a
method of a class? i.e. setting the method eh() of class foo as error
handler in the snippet below.

class foo {
function eh()
{ do something }
}
}

Further more, how can I route E_ERROR & E_PARSE errors to my custom
handler? I've tried 'error_reporting(E_ALL)' but non avail.

TIA

Jul 16 '05 #4
to**@marston-home.demon.co.uk (Tony Marston) wrote in message news:<75**************************@posting.google. com>...
Why on earth does your error handler have to be a method in a class?
Error handling is a single stand-alone process so creating a class
with a single method is total overkill. You can achieve all the error
hanfling you want in a standard function. Take a look at
http://www.tonymarston.net/php-mysql/errorhandler.html for a working
example.

I have reasons on doing it in the 'class' way. Hey, Tell you the
truth, I'm not that fond of classes either. But in this particular
project, I thought implementing it in a class would save me headaches
later. Also, it won't be a 'single' method class w/o any props. There
are tons of it.

I have already a (partially) 'functional' error handler defined in
standard 'procedural' way. Now the question I believe still unanswered
is how to route E_ERROR & E_PARSE errors to the custom handler.
error_reporting(E_ALL) (or any other veriation of arguments) doesn't
do that for me.

TIA
Jul 16 '05 #5
xd*****@zworg.com (Xeon) wrote in message news:<78**************************@posting.google. com>...
to**@marston-home.demon.co.uk (Tony Marston) wrote in message news:<75**************************@posting.google. com>...
Why on earth does your error handler have to be a method in a class?
Error handling is a single stand-alone process so creating a class
with a single method is total overkill. You can achieve all the error
hanfling you want in a standard function. Take a look at
http://www.tonymarston.net/php-mysql/errorhandler.html for a working
example.

I have reasons on doing it in the 'class' way. Hey, Tell you the
truth, I'm not that fond of classes either. But in this particular
project, I thought implementing it in a class would save me headaches
later. Also, it won't be a 'single' method class w/o any props. There
are tons of it.

I have already a (partially) 'functional' error handler defined in
standard 'procedural' way. Now the question I believe still unanswered
is how to route E_ERROR & E_PARSE errors to the custom handler.
error_reporting(E_ALL) (or any other veriation of arguments) doesn't
do that for me.

TIA


If you read the PHP manual at
http://www.php.net/manual/en/functio...or-handler.php you will
see where it says the following:

<quote>Note: The following error types cannot be handled with a user
defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR and E_COMPILE_WARNING. </quote>

As the SET_ERROR_HANDLER can only be directed to a function, why don't
you use this function to call your object method?

Tony Marston
http://www.tonymarston.net/
Jul 16 '05 #6
to**@marston-home.demon.co.uk (Tony Marston) wrote in message news:
As the SET_ERROR_HANDLER can only be directed to a function, why don't
you use this function to call your object method?

Yes I have thought of it. The problem was finding the class instance.
I have a working prototype now by searching (in the 'brute force'
kinda way) the $GLOBAL array. Any pointers on how to effectively find
a class instance?

TIA
Jul 16 '05 #7
xd*****@zworg.com (Xeon) wrote in message news:<78*************************@posting.google.c om>...
to**@marston-home.demon.co.uk (Tony Marston) wrote in message news:
As the SET_ERROR_HANDLER can only be directed to a function, why don't
you use this function to call your object method?

Yes I have thought of it. The problem was finding the class instance.
I have a working prototype now by searching (in the 'brute force'
kinda way) the $GLOBAL array. Any pointers on how to effectively find
a class instance?

TIA


What do you mean by "find a class instance"? Surely when you create a
class instance with $objectname = new classname you use a meaningfull
object name which you can reference anywhere in your code.

Tony Marston
Jul 16 '05 #8
Xeon wrote:
Yes, assuming that I refference it after the class is instantiated.
But, being a lazy coder, I want it to be generic, so without any
further coding, when I instantiated the class, the handler is
automatically routed.

Example: in my class definition code
class eh_foo {
function eh_foo() {
set_error_handler("route_handler");
}
function eh() {
//some code
}
}
function route_handler() {
// find class instance, store it in $foo variable
// then call the method eh()
$foo->eh()
}

Now, in finding the instance in a 'brute forece' way, I transverse the
$GLOBALS array and test each of it's key wether it's an object, and if
it is, if it belong to the class 'eh_foo'.

While talking about classes, I wonder if it's possible for the
constructor to refuse instantiation based on certain criteria, or said
it differently, controls when 'new' operator fails or success.

TIA


What about using the "class::method()" form to call your method? That
way, the class doesn't need to be instantiated, so you won't need to
search for an instance of it first.

~ Ryan

Jul 16 '05 #9
Actually... check out some of the user comments for set_errpr_handler()
(http://us4.php.net/manual/en/functio...r-handler.php). A few of
them specifically mention classes with error-handling methods.

~ Ryan

Jul 16 '05 #10
Xeon wrote:
Hi,

Is there anyway to set a custom error handler which is actually a
method of a class? i.e. setting the method eh() of class foo as error
handler in the snippet below.

class foo {
function eh()
{ do something }
}
}

Further more, how can I route E_ERROR & E_PARSE errors to my custom
handler? I've tried 'error_reporting(E_ALL)' but non avail.

TIA


http://au.php.net/set_error_handler

"Note: Instead of a function name, an array containing an object
reference and a method name can also be supplied. (Since PHP 4.3.0)"

and you already knew:

"Note: The following error types cannot be handled with a user defined
function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR and E_COMPILE_WARNING."

and on the same page in the example it shows you how to route:

"
// redefine the user error constants - PHP 4 only
define ("FATAL", E_USER_ERROR);
define ("ERROR", E_USER_WARNING);
define ("WARNING", E_USER_NOTICE);

// set the error reporting level for this script
error_reporting (FATAL | ERROR | WARNING);
"

Jul 16 '05 #11
R.Lange wrote:
Xeon wrote:
Yes, assuming that I refference it after the class is instantiated.
But, being a lazy coder, I want it to be generic, so without any
further coding, when I instantiated the class, the handler is
automatically routed.

Example: in my class definition code
class eh_foo {
function eh_foo() {
set_error_handler("route_handler");
}
function eh() {
//some code
}
}
function route_handler() {
// find class instance, store it in $foo variable
// then call the method eh()
$foo->eh()
}

Now, in finding the instance in a 'brute forece' way, I transverse the
$GLOBALS array and test each of it's key wether it's an object, and if
it is, if it belong to the class 'eh_foo'.

While talking about classes, I wonder if it's possible for the
constructor to refuse instantiation based on certain criteria, or said
it differently, controls when 'new' operator fails or success.

TIA

What about using the "class::method()" form to call your method?
That way, the class doesn't need to be instantiated, so you won't need
to search for an instance of it first.

~ Ryan


Jul 16 '05 #12
Let's try adding a bit of content to my emails before hitting the SEND
shortcut key ;) (sorry)

Ahem. I'm going to assume that you want to "route" the error handling
functionality to a method on the instance of the [error] triggering
object. This is different from the note about version 4.3.0 which says
you can specify a class/method by supplying an array rather than a
string to set_error_handler();

So if $objFoo triggers an error, then you want to run say
$objFoo->Throw($errno, $errmsg, $filename, $linenum, $vars);

The short answer is you can't - because you don't [RELIABLY] know the
name of of $objFoo at runtime.

The long answer is, you probably can most of the time but it will only
be under cirtain conditions and will be error prone if used generically.

One alternative is to use one object instance in your document through
which you can route all errors. Here is my error routing function.

function ErrorRouter($errno, $errmsg, $filename, $linenum, $vars) {
global $objAppDoc;
if(is_object($objAppDoc)) {
header("Content-Type: application/xml;");
$objAppDoc->throw($errmsg,0,$errno);
echo $objAppDoc->xmlGetDoc();
exit;
} else {
die($errmsg);
}
}

and here is the source code for throw()

/**
* Uniform error logger
*
* This method runs a backtrace and recursively produces an XML
fragment
* from the result of a backtrace. This can be used by debug
templates in
* the XSLT stylsheet.
*
* @param string Message to record and display as the error
* @param string It's up to the calling class to implement an
error level
* @param string It's up to the calling class to implement an
error code
* @return void
* @access private
*/
function throw($strErrMsg,$lvl="",$code="") {
$blnDoBt = true;
// create the base ERROR element
$elErr = $this->objDoc->create_element("error");
$this->ndRoot = $this->objDoc->document_element();
$ndErr = $this->ndRoot->append_child($elErr);
$ndErr->set_attribute("ts",time());
if($lvl) $ndErr->set_attribute("level",$lvl);
if($code) $ndErr->set_attribute("code",$code);
// create the MSG sub-element
$ndMsg = $this->objDoc->create_element("msg");
$ndMsg = $ndErr->append_child($ndMsg);
$ndMsg->set_content($strErrMsg);
// create the STACK sub-element
if($blnDoBt) {
$elBt = $this->objDoc->create_element("stack");
$ndBt = $ndErr->append_child($elBt);
$arrBt = debug_backtrace();
// this will create a nested call
// element for each function
call stored
// in the backtrace array.
foreach($arrBt AS $arrCall) {
$elCall = $this->objDoc->create_element("call");
foreach($arrCall AS $attCall=>$valCall) {
if(is_array($valCall)) {
if(count($valCall)) {
$elArgs =
$this->objDoc->create_element($attCall);
$ndArgs = $elCall->append_child($elArgs);
foreach($valCall AS $attArg=>$valArg) {
if(!is_string($valArg))
// delve no further
$valArg = "[".@(string)$valArg."]";
if(strlen(trim($valArg)) && $valArg !=
"[]") {
// attempt to show content of
argument
$elItem =
$this->objDoc->create_element("item");
if(strlen($valArg) > 256)
$valArg =
substr($valArg,0,255)."...";
$elItem->set_content($valArg);
$ndItem =
$ndArgs->append_child($elItem);
}
} // end foreach on call arguments
}
}
else {
// translate backtrace array
keys/vals
// to atribute name/vals for
each CALL
// element
$elCall->set_attribute($attCall,$valCall);
}
} // end foreach on call attributes

// attache the CALL element to the
// stack element.
if(isset($ndCall)) {
// nest this element under the
previous
// CALL element if one already
exists
$ndCall = $ndCall->append_child($elCall);
}
else {
// create the start CALL
element node.
$ndCall = $ndBt->append_child($elCall);
}
} // end foreach on calls
} // end foreach on backtrace
}

and this is an example of the output it produces.


<error ts="1059025526" code="8">

<msg>Undefined index: code</msg>
<stack>
<call file="c:\apache\htdocs\str2\admin\class_DomDoc.php "
line="1159" function="throw" class="strdoc" type="-&gt;">
<args>
<item>Undefined index: code</item>
<item>[0]</item>
<item>[8]</item>

</args>
<call function="errorrouter">
<args>
<item>[8]</item>
<item>Undefined index: code</item>
<item>c:\apache\htdocs\str2\admin\class_DB2xml.php </item>
<item>[102]</item>

<item>[Array]</item>
</args>
<call file="c:\apache\htdocs\str2\admin\class_DB2xml.php "
line="102" function="db2xml">
<call file="c:\apache\htdocs\str2\class_subjects.php"
line="13" function="db2xml" class="subjects" type="-&gt;">
<args>
<item>[Object]</item>
</args>
<call file="c:\apache\htdocs\str2\subjects.php" line="19"
function="subjects" class="subjects" type="-&gt;">

<args>
<item>staff</item>
</args>
<call file="c:\apache\htdocs\str2\sk.php" line="187"
function="include_once">
<args>
<item>c:\apache\htdocs\str2\subjects.php</item>
</args>
</call>

</call>
</call>
</call>
</call>
</call>
</stack>
</error>

Jul 16 '05 #13

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

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.