[PHP] Re: Globally accessible objects without using 'global $obj;'

2004-11-12 Thread Red Wingate
Hi Chris,
Something that worked out very well for me was using a function that
would take care of the error handling ... kind of
?php
define ( 'ERR_TYPE_ERROR' , 1 ) ;
define ( 'ERR_TYPE_WARNING' , 2 ) ;
class Error {
// error handling code here
}
function _handleError ( $msg , $type ) {
   static $errClass ;
   if ( is_class ( $errClass ) === FALSE ) {
  $errClass = new Error();
   }
   // hand $msg to $errClass here ...
}
function raiseError ( $msg ) {
   return _handleError ( $msg , ERR_TYPE_ERROR ) ;
}
function raiseWarning ( $msg ) {
   return _handleError ( $msg , ERR_TYPE_WARNING ) ;
}
?
But today i prefer using PHPs build-in Error-Handling functions
using the 2 Errorlevels ( E_USER_NOTICE, E_USER_WARNING
and E_USER_ERROR ) for my debugging and error-handling.
But looking at PHP5's try-throw-catch constructs makes me think
about rewritting large parts of my current project :-/
 -- red
Chris W. Parker wrote:
Hello,
Ok so I'm trying to write an error reporting class and I'm just sort of
experimenting at this point (comments outside the scope of this email
are welcome btw) with how to best implement it. One idea I had (which
isn't working out as well as I'd hoped thus far) is to create an
instance of my error class (incidentally called 'Error') at the start of
each page and then reference that instance from within all other objects
and functions.
Maybe this would be best explained with some code:
?php
$e = new Error();
function my_function()
{
// do stuff
if($this == $that)
{
$e-RaiseError();
}
}
?
Of course, that doesn't work because my_function() doesn't know anything
about $e unless I put a special statement into my_function().
?php
function my_function()
{
// here's the new line
global $e;
// do stuff
...
}
?
I already don't like the idea of having to put 'global $e;' within each
and every function/method I write. Should I abandon this idea and try
something else? If so, what?
Here is my first idea just fyi.
?php
function my_function()
{
$e = new Error();
// do stuff
if($this == $that)
{
$e-RaiseError();
}
else
{
// continue like normal
}
// get ready to return a response
if($e-error_count  0)
{
return $e;
}
else
{
// return something else
}
}
?
Now to use my_function() within a page I have to always assign the
result to a variable so that I can determine whether or not any errors
occured within the function.
?php
$result = my_function();
if($result-error_count  0)
{
// an error occurred
$result-DisplayErrors();
}
?
But that also seems like a hassle (but maybe it's a completely necessary
hassle?). I've looked around the internets for a while but have not
found any REALLY useful documents on managing errors (could be that I've
not found the write article yet) and such so any
comments/pointers/links/etc. are very welcome.
What I'd ultimately like to do is be able to return more than just a
true/false from a function regarding it's end state. For example, a
function could fail for multiple reasons and just returning a plain
false for all situations does not suffice.
Thank you for making it this far.
Chris.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Re: Globally accessible objects without using 'global $obj;'

2004-11-12 Thread Greg Beaver
Chris W. Parker wrote:
What I'd ultimately like to do is be able to return more than just a
true/false from a function regarding it's end state. For example, a
function could fail for multiple reasons and just returning a plain
false for all situations does not suffice.
You could either return an object like PEAR_Error 
(http://pear.php.net/manual/en/core.pear.pear-error.php) or use a more 
customizable solution like PEAR_ErrorStack 
(http://pear.php.net/manual/en/core.pear.pear-errorstack.php).  Also, 
don't forget about built-in options like trigger_error and Exceptions in 
php5.

More important than what you use is planning for future growth.  Your 
applications will get larger.  It may seem like a time-saver to do a 
quickie fix that requires less initial typing, but I guarantee you will 
run into a brick wall when you do have to change things and be forced to 
rewrite everything - not exactly a timesaver, unless you simply give up 
on making your code better :)

Greg
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php