Not sure if this is something allowed in PHP. I am looking to have a variable constant
$string = 'SETUP_FEE';
dd(Finance::$string);
This is basically what I am trying to do, is to get the class constant value of the string. This is now getting an error, is there a way to make it or this is not allowed by the PHP nature.
Thank you very much, I checked it, but none of the threads talking about a variable constant. What I am trying to get is different constants based on variables.
@ehabafia if you have multiple related values you'd be better off defining these as an array and then having a method to get the value based on array key.
@ehabafia when you say @geordiejackson approach didn't work, did you get any errors? The most common would be not adding the fullnamespace to the model.
@automica Yes, it is giving undefined variable Finance::$string
Regarding the namespace, I did use it at the top, and just to give you a hint, when I tried to dd(Finance::SETUP_FEE); I can see the result which is Setup Fee
So, I am not sure, it seems that PHP doesn't accept a variable constant.
@jlrdw No, in my case what's after Finance is a constant. Here is an example:
class Finance extends Model
{
// Messages
public const FINANCE_ISSUED = 'Finance Issued';
public const DUE_DILIGENCE_FEE = 'Due Diligence Fee';
public const SETUP_FEE = 'Setup Fee';
}
So, from another class:
use App\Finance;
class AchAgreementService
{
public Finance $finance;
public function __construct(Finance $finance)
{
$this->finance = $finance;
}
public function getFees($string)
{
...
dd(Finance::$string);
}
}
public function getFees($string)
{
// pseudocode
if $string == Finance Issued do this
if $string == Due Diligence Fee do that
if $string == Setup Fee do something else
etc
}