Technical limitations aside, you may think twice about enabling such programming (I suppose) to non-programmers. They will probably be randomly messed up and you will need to clean up the mess. At the very least, protect it with lots of tests. And, possibly, legal.
But you asked a question, so I will try to answer this question. There is a distinction between internal style DSL (which most people mean when they use the word DSL) and then external style DSL (which is more like a mini-language). Ruby is famous for having syntax that lends itself well to internal DSL. PHP, on the other hand, is pretty bad in this regard.
However, you can still do some things in PHP. The simplest thing is probably just to write a library of functions, and then your clients write code in simple PHP using this library. Of course, you will have to check the code, but this will give all the advantages of using the existing runtime.
If this is not enough, you will have to dig in to heavy things. First you need a parser. If you know how to do this, they can be written quite easily, but if you were not forced to write a book at school or you had a strange hobby for writing such material just for fun (I know), this will probably lead you to a bit work. The main components of the analyzer are a tokenizer and some automata (state machine) that place tokens in a tree structure (AST).
Once you have your own structure, you should evaluate it. Since this is DSL, the number of functions is limited, and performance is probably not the biggest problem, you can write some object-oriented code around the AST and leave it to that. Otherwise, you have options such as writing some kind of interpreter or cross-compiling it into another format (PHP will be the obvious choice).
All this complex part is mainly related to handling extreme cases, such as syntax errors and reporting something meaningful to the user. Again, simply by providing access to a subset of PHP, you will get it for free, so keep that in mind first.
troelskn
source share