Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ehabafia's avatar

variable constant

Hello everyone,

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,

0 likes
15 replies
ehabafia's avatar

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.

Finance::$variable;
rezuankassim's avatar

Don't know whether this is the best practice way to do it.

In your model file

Finance.php

public static function getConstantField($variable)
{
	return constant('self::' . $variable);
}

add this function and when you want to get the constant field just call the function

dd(Finance::getConstantField('SETUP_FIELD'));
GeordieJackson's avatar

Normally, you set a constant in your model:

class ClassName extends Model
{
    const FIRSTNAME = "First";
    const SECONDNAME = "Second";
}

and then access them this way:

ClassName::FIRSTNAME

ClassName::SECONDNAME

ADDED: This answer is incorret

So, if your $variable equals `"FIRSTNAME" then you can access the constant with:

ClassName::$variable

and you'll get "First" returned. And so on...

automica's avatar

@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.

<?php
class ClassName extends Model
{

    const VALUES = [
        'FOO' => 'foo value',
        'BAR' => 'bar values'
    ];

    /**
     * @param string $key
     * @return string
     */
    public static function getConstantValue(string $key): string
    {

        $values = self::VALUES;

        return isset($values[$key]) ? $values[$key] : '';
    }
}
ehabafia's avatar

@geordiejackson I assumed like you that it should work that way but I don't know why it didn't.

@automica That become so, much of a hassle. I don't think it's worth it.

automica's avatar

@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.

eg App\Model\ClassName etc

ehabafia's avatar

@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.

automica's avatar

@ehabafia the problem with just passing a variable is that it doesn't give you any error checking.

much better to have a static method and call that to get your constant, like either @rezuankassim or my approaches.

jlrdw's avatar
const MYSTRING = 'SETUP_FEE';

Finance::MYSTRING

But why not just use:

Finance::SETUP_FEE    // directly
ehabafia's avatar

@jlrdw The problem is that the $string is a variable that I am getting, so I need to call the constant according to the value of $string.

jlrdw's avatar

@ehabafia in your example:

        $string = 'SETUP_FEE';

	dd(Finance::$string);

Finance is a model, correct? What is SETUP_FEE?

I ask because normally after the Scope Resolution Operator is a method, i.e.,

$pets = Pet::getPets($petsearch);

Or a model db function:

$pet = Pet::find($petid);

Where getPets is a query scope. But just asking. Can you give your use case?

ehabafia's avatar

@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);
    }
}
GeordieJackson's avatar
Level 18

My previous answer was incorrect - posting without checking first!

If you want to access a class constant with a variable use:

$var = "FIRSTNAME"

constant(ClassName::class."::".$var)

That should do it.

1 like
jlrdw's avatar

Or you may need some if statements:

    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
    }

Please or to participate in this conversation.