Search code examples
phplanguage-agnostic

Composition vs. Inheritance dependency injection


I have a Parser with three classes of objects: the parser itself, Tokens, and States. The Parser generates tokens from the lexer. Everything is black boxed, so the tokens know nothing about the parser state or the parser, and the state knows nothing of the tokens. A fairly simple version of the arrangement:

class Parser {
   public function parse() {
      $this->state = new StEmpty;
      while ($token = $this->lexer->get()) {
         $this->state = $this->token->expect($this);
      }
   }
   public function stateStart() {
      return $this->state->stateStart();
   }
}
class StartToken {
   public function expect(Parser $parser) {
      return $parser->stateStart();
   }
}
class StEmpty {
   public function stateStart() {
      return new StStart;
   }
}

The problem I'm running into is that sometimes when a state changes, the parser needs to take some action (such as add a rule to the tree when the ending-rule token is reached). Only the State knows that, so it is up to the state to tell the parser what to do. Problem is getting the Parser to the State. I could inject the Parser in the state constructor, but not every State needs the parser, and that would lead to a lot of duplicate code (unless I had a base class for the States and the Parser was a protected member, but I want to avoid extending anything). I could also inject the Parser in the state methods that need it, but I have a similar problem: that would be a lot of duplication, and not all of the State implementations would need the parser for given methods.

So my question is how can I get the State to know about the Parser when it needs to without unnecessary inheritance or code duplication? If I need another class that's perfectly acceptable.


In case this is difficult to follow, here is an "unraveled" version:

class Parser {
   public function parse() {
      $this->state = 'StEmpty';

      while ($token = $this->lexer->get()) {
         switch ($token) {
            case 'StartToken':
               switch ($this->state) {
                  case 'StEmpty':
                     $this->state = 'StStart';
                     break;
               }
               break;
         }
      }
   }
}

The answer to this question could apply to other languages as well, but I know this would be easier to do in languages that allow overloading. PHP does not.


Solution

  • PHP 5.4 introduces traits: http://php.net/manual/en/language.oop5.traits.php

    Perhaps you could use traits as a halfway between inheritance and injection.