Have a look at Runkit_Sandbox: http://php.net/manual/en/runkit.sandbox.php
Instantiating the Runkit_Sandbox class creates a new thread with its own scope and program stack. Using a set of options passed to the constructor, this environment may be restricted to a subset of what the primary interpreter can do and provide a safer environment for executing user supplied code.