Blog2011 ≫ Farewell missing method magic, we hardly new you

Doing a bit of a

(/wiki/#code) audit at [work](/wiki/#work) and going to remove this __call method from all my

(/wiki/#code). I put it in so I could do swanky ruby on rails style magic methods like

$items = $this->fetch_bundle_by_code_and_name( 'LGWHIL', 'champagne' )

without having to write the method with that name. But it's a bit slower and I never actually used it so it's going. Saving it here for prosperity. Maybe I'll use it again one day:

  public function __call( $name, $param ) {
    $return = '';
    $regexp = '/_(by|and)_(' . implode( '|', array_keys( $this->fields )) . ')/';
    if (( strpos( 'fetch_' . $this->table . '_', $name ) == 0 ) and ( preg_match_all( $regexp, $name, $m ))) {
      $params_for_fetch = array();
      foreach ( $m[2] as $key => $var ) {
        $params_for_fetch[ $var ] = $param[ $key ];
      }
      if ( count( $params_for_fetch )) {
        $return = $this->fetch( $params_for_fetch );
      }
    }
    return $return;
  }
⬅️ :: ➡️

Paul Clarke's blog - I live in Hythe in the deep South. Married + dad to two, I'm a full stack web engineer, + I do js / Node, some ruby, other languages etc. I like pubs, running, eating, home automation and other diy jiggery-pokery, history, genealogy, Television, squirrels, pirates, lego, + TIME TRAVEL.