david19's avatar

Check if Constant exists with defined not work

Hello Team.

if(defined(User::MYCONST)){
    run my code here.....
}

The problem, i still get the error "Undefinded" Constant in the User model. But why, i check if is defined or not. Many thanks.

0 likes
6 replies
MichalOravec's avatar

I think you have to define full class name.

if (defined('\App\Models\User::MYCONST')) {
    // run my code here.....
}
1 like
david19's avatar

@tykus @michaloravec

Yes, with

defined('User::MYCONST')

works! But the problem is, "User is a variable in my case".

defined('$model::MYCONST')

Do you have any idea?

tykus's avatar
tykus
Best Answer
Level 104

You can get the class of the variable using get_class($user), e.g.

defined( get_class($user).'::MYCONST')
1 like
MichalOravec's avatar
if (defined('\'.get_class($model).'::MYCONST')) {

}
1 like

Please or to participate in this conversation.