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

2
  • 1
    Possible duplicate of Dynamic constant name in PHP Commented Feb 25, 2016 at 5: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. Commented Feb 25, 2016 at 5:16

1 Answer 1

7

I found the answer my self by doing:

echo constant('App\Utilities\Constant::WEEK_'.$num);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.