Defining class constants in facades

I have several libraries defined for my Laravel application that expose constants.

For example, I have a class for calculating holidays and working days, used to calculate the number of working days for some reports.

The definition of my class is as follows:

<?php namespace MyApp\Libraries; /** * The Holiday Calculation Helper Library * * A helper class to calculate holidays and working days. */ class Holidays { /** * The defined EBS holidays: New Years Day */ const NEW_YEARS_DAY = "new years day"; /** * The defined EBS holidays: Independence Day */ const INDEPENDENCE_DAY = "independence day"; /** * The defined EBS holidays: Christmas Day */ const CHRISTMAS_DAY = "christmas day"; ... 

They are used (for example) by the date method, which takes the value of the holiday constant and year and returns the date of this holiday this year. I have a facade and a service provider, so this library can be used in Laravel Way & trade; . Everything works fine, I have unit tests for everything, and I'm happy with the code.

I have a question on how to access these constants. If I use the facade and calling the library from other parts of my code, it looks like this:

 $xmas = \Holidays::date(\MyApp\Libraries\Holidays::CHRISTMAS_DAY, "2014"); 

This works, but I would prefer to use the facade to do this, for example:

 $xmas = \Holidays::date(\Holidays::CHRISTMAS_DAY, "2014"); 

One solution that I was thinking about is to define constants in the facade. This works, but then I separate the constant values ​​from the library - for obvious reasons, I would prefer to store the values ​​with the code with which they are associated.

Then I am in a different solution: define the constants as described above, then refer to them on the facade as follows:

 <?php namespace MyApp\Facades; use Illuminate\Support\Facades\Facade; class Holidays extends Facade { /** * The defined EBS holidays: New Years Day */ const NEW_YEARS_DAY = \MyApp\Libraries\Holidays::NEW_YEARS_DAY; /** * The defined EBS holidays: Independence Day */ const INDEPENDENCE_DAY = \MyApp\Libraries\Holidays::INDEPENDENCE_DAY; /** * The defined EBS holidays: Christmas Day */ const CHRISTMAS_DAY = \MyApp\Libraries\Holidays::CHRISTMAS_DAY; ... 

Now I can refer to constants through the facade, and not to a fully functional library class, and I need to determine the value for the constant once (although I need to add any new constants to the library and facade). This works, and I get what I want, but it's a bit like violating the DRY (Do not Repeat Yourself) principle.

So here is the question. Is this the best way to do this?

+6
source share
2 answers

You can use use for the alias of your class \MyApp\Libraries\Holidays , but unfortunately using Holidays will lead to a fatal error, so I suggest imposing it as Holiday , which can also save you some confusion about the future.

Add before declaring a class declaration and after declaring your namespace ...

 use \MyApp\Libraries\Holidays as Holiday; 

Use inside your class like this ....

 $xmas = \Holidays::date(Holiday::CHRISTMAS_DAY, "2014"); 
+1
source

How can I say about loading constants, I need to add the file to the classmap directive in composer.json

For example, (just as I use) " classmap ": ["database", "Application / RolfK / Constants.php"],

The class is defined in the file.

0
source

All Articles