Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of getter methods in entities #2

Open
doublecompile opened this issue Apr 30, 2015 · 0 comments
Open

Allow the use of getter methods in entities #2

doublecompile opened this issue Apr 30, 2015 · 0 comments

Comments

@doublecompile
Copy link
Member

Xyster does allow for setValue() setter methods in entities through __set(), but it does not allow for setValue() through __get(). It would be nice to have both in there. Currently, we have subclassed the Xyster_Orm_Entity and added the following method:

  /**
   * Override __get() to call getName() if available.
   * The implementation of Xyster_Orm_Entity does allow for
   * implementing a setter method in the entity, but it doesn't allow
   * for a getter.
   * 
   * @param string $name
   * @return mixed The value of the field
   * @throws Xyster_Orm_Entity_Exception if the field is invalid
   */
  public function __get($name)
  {
    // Check if a getter is implemented. If yes, then call it.
    if (array_key_exists($name, $this->_values)) {
      $method = 'get'.ucfirst($name);
      if (method_exists($this, $method)) {
        return $this->$method();
      }
    }

    // Otherwise, hand over control to the parent getter.
    return parent::__get($name);
  }

It would be nice if such feature would be incorporated in Xyster. We only have allowed setter methods for the values that are inside the entity, because we didn't yet see the implications of allowing a getter for any property and because we only needed support for the direct data values. Maybe it would have been okay to allow any kind of getter by doing it somewhat like this:

  public function __get($name)
  {
    // Check if a getter is implemented. If yes, then call it.
    $method = 'get'.ucfirst($name);
    if (method_exists($this, $method)) {
      return $this->$method();
    }

    // Otherwise, hand over control to the parent getter.
    return parent::__get($name);
  }

One possible conflict that I see: there is a method named getErrors() in the Entity class, so a getter for an "errors" field could yield unexpected results.
Reporter: Maurice Makaay
Begin: 2009-08-24
Completed: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant