Skip to content

Instantly share code, notes, and snippets.

@mchelen
Last active January 29, 2016 03:04
Show Gist options
  • Select an option

  • Save mchelen/096fadcdfe0f45fabcfb to your computer and use it in GitHub Desktop.

Select an option

Save mchelen/096fadcdfe0f45fabcfb to your computer and use it in GitHub Desktop.
exception handling in constructor demo
vendor
composer.lock
example.log
{
"name": "example/example",
"description": "an example",
"require": {
"monolog/monolog": "^1.17"
},
"autoload": {
"psr-4": {
"example\\example\\": "Foo"
}
}
}
<?php
namespace Example;
class Foo {
private $logger;
public function __construct(\Psr\Log\LoggerInterface $logger) {
$this->logger = $logger;
}
public function bar($baz) {
$this->logger->addDebug($baz);
}
}
<?php
require "vendor/autoload.php";
require "Foo.php";
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('example.log', Logger::DEBUG));
$foo = new Foo($log);
$foo->bar("qux");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment