D
D
Dilik Pulatov2017-05-21 23:19:45
Yii
Dilik Pulatov, 2017-05-21 23:19:45

Problem in Yii2 const how to specify correctly?

Hello!
you can not do it this way?

class MyModel extends \yii\db\ActiveRecord{
    const FILE_URL = Yii::getAlias('@webroot').'/file/myfile.txt';
}

I get an error - "Constant expression contains invalid operations"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-05-22
@webinar

It is possible like this:

class MyModel extends \yii\db\ActiveRecord{
    const FILE_URL = '/file/myfile.txt';
    
    public static function getFileUrl(){
        return Yii::getAlias('@webroot').self::FILE_URL;
   }
}

Although I do not see the point in this, but in any case, a constant should be a constant. It is impossible to shove a variable into it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question