Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Return Object In Component, Override Magic Setter/getter

$
0
0
This is hard to explain for me but I'll do my best.

I am starting to write a CApplicationComponent to get config params from DB.
<?php

class parameters extends CApplicationComponent
{
  public $cacheAll = false;
  public $dbConnectionId = null;
  public $applications = array();

  public function __get( $attr )
  {
    if( $this->applications && in_array($attr, $this->applications) )
    {
      if( Yii::app()->$$this->applications[$attr]['id'] )
      {
        //in work...
      }
    }
  }
}


public $applications would be an array like this:

$arrray = array (
  'productName' => array (
    'tableName'=>'settings',
    'property'=>'param1'
   ),
);


Well... the fact is that I need, or mostly want, to get an attribute like this:

Yii::app()->parameters->productName->param1


I know that Yii implements magic getter and setters for class properties, so I would access $applications this way:

Yii::app()->parameters->applications


So, I am now overriding the __get function, to see if the requested value is a key in the array $applications, or else I will delegate it to the Yii's defined one with parent::__get($attr).

If the requested value is a key in the $applications array, then I would have to create an object so I can use the -> operator, and another magic setter/getter, but I really don't know how yet.

IS there a better way of doing it? Can anyone point me in the right direction?

Thanks a lot

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>