Behaviors adding attribute accessors

I’ve been experimenting with behaviors and trying to learn how this works, to see how I can make use of this.

Using behaviors, I could, for example, create a CTaggable class, and simply attach that to any component to add methods for tag management.

But when I add methods like getTags() and setTags(), these do not function as accessors - that is, I can’t do $object->tags and get the return value of getTags() that way.

Seems like this would be a natural and useful feature to have - is there a good reason why it doesn’t exist?

I needed some sort of feature like this earlier. I would be happy to see this discussed more.

http://www.yiiframework.com/forum/index.php?/topic/3784-behaviors-could-use-improvments/

I tried adding support for this in CComponent … here’s the code to support getters:




  public function __get($name)

  {

    $getter='get'.$name;

    if(method_exists($this,$getter))

      return $this->$getter();

    else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))

    {

      // duplicating getEventHandlers() here for performance

      $name=strtolower($name);

      if(!isset($this->_e[$name]))

        $this->_e[$name]=new CList;

      return $this->_e[$name];

    }

    else if(isset($this->_m[$name]))

      return $this->_m[$name];

    else

    {

      try

      {

        $value = $this->__call($getter,array());

        return $value;

      }

      catch (CException $e)

      {}

    }

    throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',

      array('{class}'=>get_class($this), '{property}'=>$name)));

  }



It seems to work nicely.

Haven’t done it for setters yet - I’d like to hear more thoughts/comments on this idea, and this approach as a possible solution, before continuing…

Does this help?

http://code.google.com/p/yii/source/detail?r=1417