Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a const property inside a Class in Laravel using a dynamic string

Tags:

php

laravel

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

like image 974
John Roca Avatar asked Feb 25 '16 03:02

John Roca


1 Answers

I found the answer my self by doing:

echo constant('App\Utilities\Constant::WEEK_'.$num);
like image 86
John Roca Avatar answered Oct 31 '22 14:10

John Roca