SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP4's lack of exceptions

    I am stuck with PHP4 and I really miss exceptions. The more I think about it, the more I realize I just hate the way I've been doing error handling in all of my previous work - the code is just littered with redundant error checks. I decided to not do this in my next project.

    The solution may seem obvious - set_error_handler() and trigger_error() whenever you need to. Since I really don't care about files, line numbers indicating where the error occured, I would prefer using my own class. The call could be something like this (to be used in any class):
    PHP Code:
    ErrorHandler::handle($error$type); //$error - error ID 
    The error handler could then pick a view to display the error.

    It may be ok when you have a website in a single language: you could pass the whole error message instead of ID to the handle() method. But I frequently need to add additional languages.

    I could:
    1. Instantiate ErrorHandler in a high level class (controller) with the right language params and pass the object around (that will probably make way too many passes).
    3. Apply Observer pattern and make the objects that can produce errors act as observables (subjects).

    Both seem to be too much for this kind of task. So far I haven't found a simple solution to this. Any ideas?

    EDIT: This post is related to this one.

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess there are two things you might want to do if you identify an error: change the program flow or collect an error message for display. I think you're going to have to use Observer if you want to keep these well separated.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as I mentioned, it seems like a too heavy solution to make all the objects that can produce errors observable. It is a solution, yes, but I'd like to find something more lightweight to use.

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReeD
    Well, as I mentioned, it seems like a too heavy solution to make all the objects that can produce errors observable.
    AFAIK, you don't need to make "all the objects" observable. In PHP4 you can call your custom error handler with trigger_error which is implemented as a kind of embeded observer itself.
    There’s more than one way to skin a cat.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •