php – What's the good practice with class constants and dependancy injection-ThrowExceptions

Exception or error:

Let’s say I have the following code

class MyLogger 
{
    const DEFAULT_KEY = 'DEF';
    const USER_KEY = 'USE';
    const BOOK_KEY = 'BOO';

    //methods dealing with logs
}

abstract class Master 
{
    protected $log_key = MyLogger::DEFAULT_KEY;

    protected do_something() {
        MyLogger::log(static::$log_key,"message");
    }
}

class User extends Master 
{
    protected $log_key = MyLogger::USER_KEY;
}

class Book extends Master 
{
    protected $log_key = MyLogger::BOOK_KEY;
}

MyLogger is responsible to deal with app’s logs, each log has a key referencing the business object it’s from
Master has methods shared with all it’s childrens, one is creating a log
Each children overrides the $log_key that should be used

Now I’m having a headache trying to make it fit in good practice
I can inject MyLogger in Master’s constructor, but I can’t find the best way to let every children use the right log_key without importing MyLogger in each of them just to be able to get the keys constants

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *