SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Observer Pattern Question - Jason's Book

    Hi,
    I was readin Chapter 9 - Observer Pattern. Jason implemented that error handler example. I have a few questions:
    1) How does it know which observer is being used??
    2) We have to use attach mehod and passit the object of any class( file log or mail), right??
    3) How do we decide which one to use? Just trying to get a feel , when would you use mailing error rather than loggin it and how do you decide when to use it??
    OR
    is it that I just use all of them the same time. I just want to be able to use only one of them somehow either loggin (less fatal errors or email for fatal errors)
    4) when i use set_error_handler, do i create a function in every script or somewhere OR i use a factory where i use that other alternative of set_error_handler
    set_error_handler(array(&$this,'error_handler'));

    Thanks
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    Hi,
    I was readin Chapter 9 - Observer Pattern. Jason implemented that error handler example. I have a few questions:
    1) How does it know which observer is being used??
    Well... the whole point of the patterns is that the observable does not need to know who is observing it or why. This is also why it is sometimes known as publish/subscribe.

    Quote Originally Posted by jaswinder_rana
    2) We have to use attach mehod and passit the object of any class( file log or mail), right??
    You would need to attach an observer. Basically any class which responds to whatever methods you invoke in your notify step.

    Quote Originally Posted by jaswinder_rana
    3) How do we decide which one to use? Just trying to get a feel , when would you use mailing error rather than loggin it and how do you decide when to use it??
    OR
    is it that I just use all of them the same time. I just want to be able to use only one of them somehow either loggin (less fatal errors or email for fatal errors)
    You would decide based on your own preferences, needs and requirements, or those of your customers.

    You are kind of out of luck with fatal error, they never hit any custom error handler, which is what the example of the observer pattern I showed was.

    Quote Originally Posted by jaswinder_rana
    4) when i use set_error_handler, do i create a function in every script or somewhere OR i use a factory where i use that other alternative of set_error_handler
    set_error_handler(array(&$this,'error_handler'));

    Thanks
    In PHP4, just be carefull with the reference, you do not want to be attaching observers to a different instance than the one the error logger is using to notify. Alternativly, I suppose you could code the observers list as some varient of the monostate pattern (see the end of the singleton chapter).

    HTH

    Regards,
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


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
  •