1

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.

MarBer
  • 535
  • 1
  • 5
  • 22

1 Answers1

1

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');
}
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thanks @scaisEdge the second solution you propose is perfect for my module configuration. For completeness I've change the const variable in a public variable but I receive the same error as the costant definition... Ideas? – MarBer Jun 17 '16 at 14:17
  • The problem is the same you can assign a dinamic value during declaration phase in PHP .. well if my answer is right please mark it as accepted – ScaisEdge Jun 17 '16 at 15:00