Home arrow PHP arrow Building Interpreter Classes with PHP 5

Building Interpreter Classes with PHP 5

If you have ever written an application that primarily parses commands -- and who hasn't? -- keep reading. As is often the case, pattern-based programming makes this task easier. In this first part of a three-part article series, you'll learn the basic concepts surrounding the Interpreter pattern, with plenty of hands-on examples.

TABLE OF CONTENTS:
  1. Building Interpreter Classes with PHP 5
  2. The basics of implementing the interpreter pattern
  3. Parsing predefined commands
  4. Seeing the interpreter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
April 02, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Introduction

In the vast and fascinating terrain of web development, working with applications whose primary task is to parse commands happens very frequently. Only think about this concept for a moment, and you'll quickly realize that every day you're dealing with some kind of parser, ranging from your own browser that interprets (X)HTML, CSS, XML, JavaScript code, etc. to your loyal PHP module that parses your neatly-crafted web applications.

Actually, the concept of a parser is VERY old, but the impressive advance of technology has brought it to us in a variety of applications that sometimes exceed our imagination. However, it's not my intention to discuss here the influence of parsers on mankind, since it'd be rather irrelevant to the purposes of this article, and possibly it'd also be boring to you.

Instead, I'd like analyze this interesting topic from a practical point of view, and ask you the following question: how many times have you found yourself developing a web application that was intended to interpret a certain number of predefined commands? Certainly, the answer might be dozens, even hundreds of times.

But, what if I extend this concept one step further and tell you that it's possible to apply it in the context of pattern-based programming with PHP? Yes, you guessed right. Among the numerous design patterns available out there, there's one called "Interpreter." As its name suggests, it has the capacity to parse specific commands to perform a set of predefined tasks.

Does this sound a bit confusing? Fear not, because building an interpreter class with PHP is a no-brainer process that can be achieved with only minor efforts. In plain terms, when the interpreter pattern is applied, on one hand there's a class that defines a set of commands that will be parsed, while on the other hand, there are some additional classes that are responsible for sending these commands to the interpreter. Now, the concept sounds much simpler, right?

Naturally, if you're anything like me and want to see different hands-on examples on how to create interpreter classes with PHP, then I've got good news for you. In this series, which is comprised of three articles, I'll provide you with numerous code samples showing how to implement this useful pattern, ranging from building a simple application for managing user information to creating a decent blogger.

Now, having introduced the subject of this series, let's start learning together how to build interpreter classes with PHP 5. It's going to be a truly instructive experience!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: