Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make constructor public instead of protected #381

Merged
merged 2 commits into from
Jun 13, 2017
Merged

Conversation

Vmak11
Copy link
Contributor

@Vmak11 Vmak11 commented Apr 25, 2017

This is needed when you want to call handler methods explicitly without registering default php error/exception/shutdown handlers. An example use case would be if you utilized your own error/exception/shutdown handlers and within that handler called these handler methods. You may also call other handler methods unrelated to bugsnag.

This is needed when you want to call handler methods explicitly without registering default php error/exception/shutdown handlers.  An example use case would be if you utilized your own error/exception/shutdown handlers and within that handler called these handler methods.  You may also call other handler methods unrelated to bugsnag.
@GrahamCampbell
Copy link
Contributor

GrahamCampbell commented Apr 25, 2017

Which method in particular did you want to call please?

@Vmak11
Copy link
Contributor Author

Vmak11 commented Apr 25, 2017

All of them. In our previous version of Bugsnag package these methods were available directly from the bugsnag client object and wasn't an issue. For instance, lets just focus on the error handler method "errorHandler" as an example. This is a very basic example of my previous explained use case but hopefully gets the point across. Sorry for any syntax errors in advance.

// just a class that has a custom method for error handling
class CustomErrorClass
{
    protected $handlers;

    public function __construct()
    {
        $this->handlers = [];               
    }

    public function addHandler(AbstractHandler $handler)
    {
        $this->handlers[] = $handler;
    }

    public function customErrorHandler($level, $message, $file, $line)
    {            
        foreach ($this->handlers as $handler) {
            $handler->handleError($level, $message, $file, $line);
        }
    } 
}

// extends AbstractHandler to ensure it has a handleError method
class CustomBugsnagHandler extends AbstractHandler
{
    public function handleError($level, $message, $file, $line)
    {
        $client = Bugsnag\Client::make('YOUR-API-KEY-HERE');
        $handler = new Bugsnag\Handler($client);
        $handler->errorHandler($level, $message, $file, $line);
    }
}

$custom_error_class = new CustomErrorClass();

// A way to add various different error handlers
$custom_error_class->addHandler(new CustomBugsnagHandler());
$custom_error_class->addHandler(new CustomLogHandler());
$custom_error_class->addHandler(new OtherCustomErrorHandler());

// Set your custom error handler, we don't want this overridden by BugSnag register method on Handler class
set_error_handler([$custom_error_class, "customErrorHandler"]);

@GrahamCampbell
Copy link
Contributor

Nice idea. I am aware that this class is non-final, so technically this could be regarded as a breaking change: https://3v4l.org/pILQK.

If @kattrali decides that this is an acceptable break, then this PR has a 👍 from me. The use case seems fair.

@kattrali kattrali merged commit 24599b4 into bugsnag:master Jun 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants