Search code examples
phpyii2yii2-advanced-appyii2-module

Yii2 strange errorException on call Yii::$app->


I'm implementing an yii2 module for handle slider inside my application, the base structur of project is yii2 advance template. I've a class Images wich extends another model class generated by gii, Images.php

<?php

 namespace common\modules\sliders\models;

 use Yii;
 use common\modules\sliders\models\base\Images as Im;

 /**
  * This is the model class for table "images".
  *
  */
 class Images extends Im
 {
    const UPLOAD_URL = Yii::$app->getModule('sliders');
 }

the costant UPLOAD_URL would be the path where I'll upload my images and this value is stored in the configuration params of my module, so that is more simple configuring the module for use in another application. When I create an instance of my Images object the strange error I receive is:

syntax error, unexpected '$app' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS)

which interest this line:

const UPLOAD_URL = Yii::$app->getModule('sliders');

Why this?

P.s.: I know that UPLOAD_URL in this way don't take the values of configurations params but I'm stopped by the error.

Thanks.


Solution

  • You can't assign dinamically a value to a constant

    you can do this

    const UPLOAD_URL ='yourpath\yourmodul\;
    

    or if you need dinamically use a function

    public  function getUploadUrl() {
      return Yii::$app->getModule('sliders');
    }