rfc:token_as_object

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:token_as_object [2020/02/14 16:22] nikicrfc:token_as_object [2020/02/25 15:58] nikic
Line 27: Line 27:
     /**     /**
      * Same as token_get_all(), but returning array of PhpToken.      * Same as token_get_all(), but returning array of PhpToken.
-     * @return PhpToken[]+     * @return static[]
      */      */
     public static function getAll(string $code, int $flags = 0): array;     public static function getAll(string $code, int $flags = 0): array;
          
-    public function __construct(int $id, string $text, int $line = -1, int $pos = -1);+    final public function __construct(int $id, string $text, int $line = -1, int $pos = -1);
 } }
 </PHP> </PHP>
Line 45: Line 45:
   * The token stores the position in the file, so that consumers don't have to compute and store it separately.   * The token stores the position in the file, so that consumers don't have to compute and store it separately.
  
-Finally (and this is the real motivation here), the tokens take up significantly less memory, and are faster to construct as well. On a large file:+Finally, the tokens take up significantly less memory, and are faster to construct as well. On a large file:
  
 <code> <code>
Line 55: Line 55:
     Time: 0.32s (for 100 tokenizations)     Time: 0.32s (for 100 tokenizations)
 </code> </code>
 +
 +==== Extensibility ====
 +
 +The ''PhpToken::getAll()'' method returns ''static[]'', as such it is possible to seamlessly extend the class:
 +
 +<PHP>
 +class MyPhpToken extends PhpToken {
 +    public function getLowerText() {
 +        return strtolower($this->text);
 +    }
 +}
 +
 +$tokens = MyPhpToken::getAll($code);
 +var_dump($tokens[0] instanceof MyPhpToken); // true
 +$tokens[0]->getLowerText(); // works
 +</PHP>
 +
 +To guarantee a well-defined construction behavior, the ''PhpToken'' constructor is final and cannot be overridden by child classes. This matches the extension approach of the ''SimpleXMLElement'' class.
  
 ===== Open Questions ===== ===== Open Questions =====
Line 102: Line 120:
  
     /** Get the name of the token. */     /** Get the name of the token. */
-    public function getTokenName(): string {+    public function getTokenName(): ?string {
         if ($this->id < 256) {         if ($this->id < 256) {
             return chr($this->id);             return chr($this->id);
 +        } elseif ('UNKNOWN' !== $name = token_name($this->id)) {
 +            return $name;
         } else {         } else {
-            return token_name($this->id);+            return null;
         }         }
     }     }
 } }
 </PHP> </PHP>
- 
-==== Allowing extension of the class ==== 
- 
-If the class is extended, should ''MyPhpToken::getAll()'' return an array of ''MyPhpToken''? How does this interact with constructors? 
  
 ===== Rejected Features ===== ===== Rejected Features =====
rfc/token_as_object.txt · Last modified: 2020/11/12 13:33 by nikic