Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:45:03+00:00 2026-06-12T17:45:03+00:00

I do know there are already many questions on Stackoverflow related to custom error

  • 0

I do know there are already many questions on Stackoverflow related to custom error handlers. But, after reading many of them, as well as the PHP manual, I am still unable to solve my problem. Thus I am posting this question.

My script is currently structured this way:

require 'file.php';
require 'anotherFile.php';
// several more "require" here. These files contain many functions

function myErrorHandler($errno, $errstr, $errfile, $errline, $errcontext){
    // some code to handle errors here
    
}

class myObject {
    function __construct() {
        // set the values here
    }

    function computeSomething() {
        ...
        doFunction2();
        ...
    }

    function SomethingBadHappened()
    {
    }
}

function doFunction1() {
    // for some reason, an error happens here
    // it is properly handled by the current error handler
}

function doFunction2() {
    // for some reason, an error happens here
    // since it got called by $obj, I want the error handler to run $obj->SomethingBadHappened();
    // but $obj is not known in myErrorHandler function!
}

set_error_handler('myErrorHandler');

// some procedural code here
doFunction1();
doAnotherThing();

// then I use objects
$obj = new myObject();
$obj->run();

// then I may use procedural code again
doSomethingElse();

My custom error handler is already working fine. It catches and processes all PHP errors that occur in the code executed after the error handler is set.

My problem:

If an error occurs within a method of myObject class, I would like to call a non-static method:

$obj->SomethingBadHappened();

$obj is not in the scope of myErrorHandler. How can I access $obj inside the error handler to call a member function of $obj?
I currently have 300KB of PHP code, and I can’t change the signature of all the functions to add $obj as a parameter (there are too many functions!).

I read that it is possible to define the custom error handler as a method of an object. But, if I do that, it won’t be able to catch errors that happen before creating the instance of myObject ($obj).

I also read about exceptions, but it doesn’t seem to help solving my problem.
I am not willing to use global variables. Here are 2 questions that explain why global variables should be avoided:

  • PHP global in functions
  • Who needs singletons?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:45:05+00:00Added an answer on June 12, 2026 at 5:45 pm

    I finally decided for this design:

    • my current error handler remains unchanged (function myErrorHandler). It holds the code to be executed when an error happens (anywhere in the code)
    • in the class myObject I added another error handler (function myObject_error_handler)
    • the constructor of myObject registers the new error handler
    • the function myObject_error_handler may now call the function SomethingBadHappened, then calls myErrorHandler to process the error

    The major benefit of doing so it that it required very few changes to the existing code:

    • one new method in my class
    • register the new error handler in the constructor
    • a call to the older error handler from the new error handler

    The new code is:

    require 'file.php';
    require 'anotherFile.php';
    // several more "require" here. These files contain many functions
    
    function myErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
        // some code to handle errors here
    
    }
    
    class myObject {
        function __construct() {
            // set the values here
    
            // register another error handler
            set_error_handler(array($this, 'myObject_error_handler'));
        }
    
        function myObject_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
            // call any number of methods of the current class
            $this->SomethingBadHappened();
    
            // then call the other error handler
            myErrorHandler($errno, $errstr, $errfile, $errline, $errcontext);
        }
    
        function computeSomething() {
            ...
            doFunction2();
            ...
        }
    
        function SomethingBadHappened() {
            // does something when an error happens
        }
    }
    
    function doFunction1() {
        // for some reason, an error happens here
        // it is properly handled by the current error handler
    }
    
    function doFunction2() {
        // for some reason, an error happens here
        // now the function myObject_error_handler will be called automatically
    }
    
    set_error_handler('myErrorHandler');
    
    // some procedural code here
    doFunction1();
    doAnotherThing();
    
    // then I use objects
    $obj = new myObject();
    $obj->run();
    
    // then I may use procedural code again
    set_error_handler('myErrorHandler');
    doSomethingElse();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are many similiar questions already asked on stackoverflow, but still,
I know there are already many questions like mine but I found no answer
I know there are many similar questions already, but I've spent hours trying to
I know there are many questions like this already on SO but none of
Hi I know that there are many questions already in SO related to my
First of all: I do know that there are already many questions and answers
First of, I know there are similar questions already on stackoverflow ( this ,
I know there are many other questions similar to this one, but none of
friends. I know, there are many questions here already on these iterators. I've read
I know that there are similar questions which are already answered, but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.