Invoking const property inside a class in Laravel using a dynamic string

I have a Constant class in App \ Utilities See below:

<?php class Constant { const WEEK_1 = 'Week 1', WEEK_2 = 'Week 2'; } ?> 

And I can repeat Constant :: WEEK_1, it gives me "Week 1",

But I want to dynamically invoke a constant week.

 foreach([1,2] as $key => $num) { echo Constant::'WEEK_'.$num } 

And I get a parsing error.

How to do it? Anyone? thanks

+5
source share
1 answer

I myself found the answer:

 echo constant('App\Utilities\Constant::WEEK_'.$num); 
+3
source

Source: https://habr.com/ru/post/1243809/


All Articles