0

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,

Dharman
  • 30,962
  • 25
  • 85
  • 135

3 Answers3

1

If you need to dynamically access a constant by using a variable you have the option of using the constant function:

$something = 'SETUP_FEE';
$const = constant("Finance::$something");

"constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function. "

PHP.net Manual - Function Reference - constant

PHP.net Manual - Language Reference - Classes and Objects - Class Constants

lagbox
  • 48,571
  • 8
  • 72
  • 83
0

If it is in a class then do this public const GREATING = 'Hello World'; ClassName::GREATING. But if it isn't then do this define("GREETING", "Hello World"); echo GREETING;

But to recreate what I think you want assuming you would like the setup fee to be modifiable:

class Finance extends Model
{
    // Am going to leave these yere for references...
    public const DUE_DILIGENCE_FEE       = 'Due Diligence Fee';
    public const SETUP_FEE               = 'Setup Fee';
    public const REGISTRATION_FEE        = 'Registration Fee';


    protected $fee_const;

    public function setFeeConst($fee)
    {
         $this->fee_const = $fee;
         return $this;
    }

    public function getFeeConst()
    {
        return $this->fee_const;
    }
}
class AchAgreementService
{
    public function issue_fees($fee)
    {
        $finance = (new Finance)->setFeeConst($fee);

        dd($finance->getFeeConst());

    }
}

But if it's going to be constant then:

class Finance extends Model
{
    public const DUE_DILIGENCE_FEE       = 'Due Diligence Fee';
    public const SETUP_FEE               = 'Setup Fee';
    public const REGISTRATION_FEE        = 'Registration Fee';

    // This is key...you cant call something you didn't set
    public const FEE_CONST        = 'Fee const';
}
class AchAgreementService
{
    public function issue_fees($fee)
    {
        $fee_const = strtoupper($fee);

        // Also pay attention to how am calling it without the dollar sign ($).
        // This is going to work
        dd(Finance::FEE_CONST);

        // It is also important to note that you cant do this
        // Finance::FEE_CONST = $fee;
    }
}

If you're going to change the value of FEE CONST then use the first approach.

tsommie
  • 470
  • 4
  • 13
  • it is defined in the Finance class, but it's not catching it. – user2909855 Sep 01 '20 at 02:06
  • set it like this `public const SETUP_FEE = 'SETUP_FEE';`. No `$`. – tsommie Sep 01 '20 at 02:07
  • check the example of my classes bellow. – user2909855 Sep 01 '20 at 02:20
  • This Finance::$fee_const is not set in the Finance class. Look closely at what your trying to do. First, you cant dynamically set a constant variable that's the whole point of declaring it as a constant. Secondly, even if you set a constant, you don't call it with a $ sign, you simply call it like so Finance::fee_const. AM I missing something else? – tsommie Sep 01 '20 at 02:48
  • Also its going to help if we know the error your getting. – tsommie Sep 01 '20 at 03:12
0

You can define the constants as constants. Then use reflection class to get the constant names in an array. Manipulate array to get what you want.

Something like this, excuse the formatting if theres any Im typing on phone.

class Finance {
 const SETUP_FEE = 'SETUP_FEE';

 static function getConstant($dynamicConstant) {
     $fin = new ReflectionClass(__CLASS__);
     $arrConst = $fin->getConstants();
     return $arrConst[$dynamicConstant];
 }
}

Now you can do

 $fee_const = 'SETUP_FEE';
 $feeConst = Finance::getConstant($fee_const); 

--- having seen lagbox's solution, this one is definitely over engineered

Avi
  • 134
  • 8