6

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

<?php
 class Constant {
   const

        WEEK_1 = 'Week 1',
        WEEK_2 = 'Week 2';
}
?>

And I can echo Constant::WEEK_1 it gives me 'Week 1',

But what I want is to dynamically call constant week say

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

And Im getting a parse error.

How to do this? Anyone? Thanks

John Roca
  • 1,204
  • 1
  • 14
  • 27
  • 1
    Possible duplicate of [Dynamic constant name in PHP](http://stackoverflow.com/questions/3995197/dynamic-constant-name-in-php) – Narendrasingh Sisodia Feb 25 '16 at 05:12
  • @Uchiha I think its not a duplicate. The link you attached is not using Laravel. And my tweek was to include the namespace. Though both using constant define function. – John Roca Feb 25 '16 at 05:16

1 Answers1

7

I found the answer my self by doing:

echo constant('App\Utilities\Constant::WEEK_'.$num);
John Roca
  • 1,204
  • 1
  • 14
  • 27