PHP VERSION: 7.0.22

anonymous_class_logger.php

<?php
// anonymous class: logger example

class Test
{
    protected 
$logger;
    protected 
$logFile;
    public function 
setLogFile($logFile)
    {
        
$this->logFile $logFile;
    }
    public function 
getLogger()
    {
        if (!
$this->logger) {
            
$this->logger $this->buildLogger($this->logFile);
        }
        return 
$this->logger;
    }
    public function 
buildLogger($logFile)
    {
        return new class (
$logFile) {
            protected 
$logFile;
            public function 
__construct($logFile)
            {
                
$this->logFile $logFile;
            }
            public function 
log($message)
            {
                
file_put_contents($this->logFile$messageFILE_APPEND);
            }
            public function 
readLog()
            {
                return 
file_get_contents($this->logFile);
            }
        };
    }
}

$test = new Test();
$test->setLogFile(__DIR__ '/test.log');
$test->getLogger()->log('TEST1: ' date('Y-m-d H:i:s'));

$logger $test->getLogger();
$logger->log('TEST2: ' date('Y-m-d H:i:s'));

echo 
$logger->readLog();

Output


TEST1: 2015-08-18 15:00:12TEST2: 2015-08-18 15:00:12TEST1: 2016-10-06 23:49:22TEST2: 2016-10-06 23:49:22TEST1: 2016-11-15 08:31:59TEST2: 2016-11-15 08:31:59TEST1: 2016-11-16 18:31:05TEST2: 2016-11-16 18:31:05TEST1: 2016-11-17 08:47:50TEST2: 2016-11-17 08:47:50TEST1: 2016-11-19 23:30:18TEST2: 2016-11-19 23:30:18TEST1: 2016-11-20 18:30:08TEST2: 2016-11-20 18:30:08TEST1: 2016-11-26 06:52:57TEST2: 2016-11-26 06:52:57TEST1: 2016-11-27 11:09:29TEST2: 2016-11-27 11:09:29TEST1: 2016-12-01 00:55:56TEST2: 2016-12-01 00:55:56TEST1: 2016-12-25 18:28:31TEST2: 2016-12-25 18:28:31TEST1: 2016-12-29 10:25:37TEST2: 2016-12-29 10:25:37TEST1: 2017-01-06 10:41:50TEST2: 2017-01-06 10:41:50TEST1: 2017-01-07 18:24:01TEST2: 2017-01-07 18:24:01TEST1: 2017-01-09 07:33:58TEST2: 2017-01-09 07:33:58TEST1: 2017-01-11 22:41:11TEST2: 2017-01-11 22:41:11TEST1: 2017-01-13 00:14:26TEST2: 2017-01-13 00:14:26TEST1: 2017-01-15 02:37:42TEST2: 2017-01-15 02:37:42TEST1: 2017-01-17 19:44:28TEST2: 2017-01-17 19:44:28TEST1: 2017-01-19 14:18:51TEST2: 2017-01-19 14:18:51TEST1: 2017-01-19 23:34:30TEST2: 2017-01-19 23:34:30TEST1: 2017-01-20 03:07:15TEST2: 2017-01-20 03:07:15TEST1: 2017-01-20 13:08:22TEST2: 2017-01-20 13:08:22TEST1: 2017-01-20 23:51:41TEST2: 2017-01-20 23:51:41TEST1: 2017-01-22 05:46:58TEST2: 2017-01-22 05:46:58TEST1: 2017-01-26 20:00:47TEST2: 2017-01-26 20:00:47TEST1: 2017-02-01 03:58:16TEST2: 2017-02-01 03:58:16TEST1: 2017-02-05 20:47:43TEST2: 2017-02-05 20:47:43TEST1: 2017-02-07 03:43:49TEST2: 2017-02-07 03:43:49TEST1: 2017-02-17 12:21:11TEST2: 2017-02-17 12:21:11TEST1: 2017-02-17 14:38:30TEST2: 2017-02-17 14:38:30TEST1: 2017-02-19 19:23:24TEST2: 2017-02-19 19:23:24TEST1: 2017-02-23 18:32:25TEST2: 2017-02-23 18:32:25TEST1: 2017-02-26 03:42:18TEST2: 2017-02-26 03:42:18TEST1: 2017-03-01 14:31:13TEST2: 2017-03-01 14:31:13TEST1: 2017-03-06 14:21:38TEST2: 2017-03-06 14:21:38TEST1: 2017-03-12 00:43:43TEST2: 2017-03-12 00:43:43TEST1: 2017-03-12 04:31:23TEST2: 2017-03-12 04:31:23TEST1: 2017-03-18 18:07:51TEST2: 2017-03-18 18:07:51TEST1: 2017-03-23 14:23:58TEST2: 2017-03-23 14:23:58TEST1: 2017-03-26 07:11:45TEST2: 2017-03-26 07:11:45TEST1: 2017-04-04 09:10:19TEST2: 2017-04-04 09:10:19TEST1: 2017-04-04 13:52:41TEST2: 2017-04-04 13:52:41TEST1: 2017-04-11 01:02:28TEST2: 2017-04-11 01:02:28TEST1: 2017-04-11 05:56:00TEST2: 2017-04-11 05:56:00TEST1: 2017-04-11 15:59:34TEST2: 2017-04-11 15:59:34TEST1: 2017-04-17 18:19:25TEST2: 2017-04-17 18:19:25TEST1: 2017-05-04 08:03:00TEST2: 2017-05-04 08:03:00TEST1: 2017-05-06 14:05:31TEST2: 2017-05-06 14:05:31TEST1: 2017-05-09 19:08:47TEST2: 2017-05-09 19:08:47TEST1: 2017-05-09 20:54:25TEST2: 2017-05-09 20:54:25TEST1: 2017-05-19 01:13:55TEST2: 2017-05-19 01:13:55TEST1: 2017-06-01 03:13:13TEST2: 2017-06-01 03:13:13TEST1: 2017-06-02 17:16:31TEST2: 2017-06-02 17:16:31TEST1: 2017-06-03 22:21:44TEST2: 2017-06-03 22:21:44TEST1: 2017-06-09 15:43:02TEST2: 2017-06-09 15:43:02TEST1: 2017-06-13 08:50:30TEST2: 2017-06-13 08:50:30TEST1: 2017-06-15 00:01:55TEST2: 2017-06-15 00:01:55TEST1: 2017-06-15 06:27:00TEST2: 2017-06-15 06:27:00TEST1: 2017-06-24 04:35:54TEST2: 2017-06-24 04:35:54TEST1: 2017-06-25 03:06:45TEST2: 2017-06-25 03:06:45TEST1: 2017-07-01 06:47:49TEST2: 2017-07-01 06:47:49TEST1: 2017-07-07 18:59:21TEST2: 2017-07-07 18:59:21TEST1: 2017-07-16 19:45:15TEST2: 2017-07-16 19:45:15TEST1: 2017-07-20 23:02:43TEST2: 2017-07-20 23:02:43TEST1: 2017-08-14 11:01:38TEST2: 2017-08-14 11:01:38TEST1: 2017-08-19 09:08:09TEST2: 2017-08-19 09:08:09
SOURCE CODE