Yii Framework Forum: Return Object In Component, Override Magic Setter/getter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Return Object In Component, Override Magic Setter/getter need advices on how to achieve something Rate Topic: -----

#1 User is offline   Jorgee 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 22-March 13

Posted 26 July 2013 - 03:56 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users