SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    Beijing
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing variables from a function within a function

    I have the following class:

    Code:
    class ErrorLog
    {
    
      var $logFile = "";
    
      function ErrorLog($fileName)
      {
        error_reporting(E_ALL);
        $this->logFile = $fileName;
    
        function handleError($type, $msg, $file, $line)
        {
          error_log("$msg", 3, $this->logFile);
        }
        set_error_handler('handleError');
      }
    }
    The problem I am experiencing is that the $logFile variable cannot be accessed from within the handleError() function. When I run this code, PHP reports an undefined variable error for the $this variable. I've tried using $logFile instead of $this->logFile, but PHP still cannot recognise the variable. I've also tried adding global $logFile into the handleError function, but it still doesn't work.

    Anybody know what I am missing here?

    Thanks for your help!

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... yeah. handleError() has no knowledge of ErrorLog or its members. Why is handleError() a sub of the constructor? Is there some reason you can't make it a class method?
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm wondering why you would ever want to do this? (function in the function). I cant think of a good reason for doing this. Can you explain your reasoning for this? (and i'm not sure this is even good programming semantics (wd?) ) Thanks.
    Erh

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    set_error_handler also accepts methods.
    PHP Code:
    set_error_handler(array(&$this'handleError')); 
    and then add a method handleError to the ErrorLog class.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    Beijing
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandibal
    I'm wondering why you would ever want to do this? (function in the function). I cant think of a good reason for doing this. Can you explain your reasoning for this? (and i'm not sure this is even good programming semantics (wd?) ) Thanks.
    Actually, I got this code directly from a user-contributed note on the PHP manual that seemed to make sense at the time. You can see the page at http://www.php.net/manual/en/functio...or-handler.php, it's the user comment posted by Thomas Staunton. Anyway, I wanted to do this because I wanted to put the error_handler function inside an error handling class, which is then called by other PHP programs to deal with errors. I did this before I realised that the set_error_handler function could actually accept an array containing an object reference and method name. So I managed to get the error_log class working now by using set_error_handler(array("className", "handleErrorFunction").

    Thanks for everyone's help!

  6. #6
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Actually, I got this code directly from a user-contributed note on the PHP manual that seemed to make sense at the time.
    Fair enough. Not sure what your take is now on the validity of doing something like this but I dont see the reason to ever complicate things by doing this. The below makes more sense as far as I can tell.
    PHP Code:
    Class Error {
     
     * Function 
    Error() {
     **  
    set_error_handler('HandleError');
     * }

       Function 
    HandleError($n$m$f$l){
     ** *  
    print_r(debug_backtrace());
     **}
     } 
    Again if some one can explain to me why this isnt better than the way the guy posted in the user comments of the manual please tell me.
    Erh


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
  •