To clarify:
I'm building a Logger class that allows me to easily log messages:
lib.Logger.php:
<?php class Logger { private $handle; public function __construct($log_name, $log_path) { if ( ! is_dir($log_path)) throw new Exception('Log path does not exist.'); if ( ! in_array(strtolower(substr($log_name, 0, -4)), array('.log', '.txt'))) $log_name = "{$log_name}.log"; $this->handle = fopen("{$log_path}/{$log_name}", 'a'); $this->log('------------- Initializing ------------- '.get_parent_class($this)); } // -------------------------------------------------------------------- public function __destruct() { fclose($this->handle); } // -------------------------------------------------------------------- public function log($message) { $time = date(DATE_RFC822); $log = "[{$time}] {$message}\n"; fwrite($this->handle, $log); } } ?> And I call this using:
MyController.php:
<?php class MyController extends Controller { $logger = new Logger('testlog','/path/to/logs/'); $logger->log('Logs are fun!'); } ?> When I initialize the object:
$this->log('------------- Initializing ------------- '.get_parent_class($this)); I want to log the name of the object (or file) that is calling log() -- in this case, either
MyControlleror
/path/to/MyController.php.
I tried using get_parent_class(), but of course this doesn't work because Logger does not have a parent class per se.
Any ideas? thank you so much for the help!
Alex B