简体   繁体   中英

How to pass extra parameters on extension's class from main config?

I have a third party class ( in extension/MYClass folder ) which constructor requires two necessity parameters.

class MyClass {
   function __construct($param1, $param2, $param3 = false) {
    $this->param1 = $param1;
    $this->param2 = $param2;
    $this->param3 = $param3;
  }
  public static function send(){
    ---------
    ---------
  }
}

When I go through the main config file to access that class like this. Then How should I pass those params on that class?

'components'=>array(
        'myclass' => array(
                    'class' => 'ext.MyClass.MyClass',
            ),
),

So that I can easily access the function like Yii::app()->myclass->send();

Firstly you should have MyClass extend CApplicationComponent if you havn't already done so.

Then it's just a matter of supplying the params a key => value pair to the config.

'components'=>array(
        'myclass' => array(
                    'class' => 'ext.MyClass.MyClass',
                    'myParam' => 'test'
            ),
),

And setting up the class properly.

   class MyClass extends CApplicationComponent {
      public $myParam
      public function send(){
         echo $this->myParam;
      }
    }

The above would echo test for the call Yii::app()->myclass->send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM