Demo
This demo is intended as a quick introduction outlining what the
current release of phc can do for you. It
does not explain everything in detail. For more information on
implementing your own tools based on phc,
read Getting Started.
The Source Program
Consider the following simple PHP script.
<?php
function foo()
{
return 5;
}
$foo = foo();
echo "foo is $foo<br>";
?>
Internally this program gets represented as an abstract syntax tree (open demo.png to see the
tree).
The Transform
Suppose we want to rename function foo to
bar. This is done by the following (C++) program:
#include "Tree_visitor.h"
class Rename_foo_to_bar : public Tree_visitor
{
void pre_method_name(Token_method_name* in)
{
if(*in->get_value == "foo")
in->value = new String("bar");
}
};
Finally, we need to instruct phc to run our
transform:
Rename_foo_to_bar f2b;
php_script->transform(&f2b);
The Result
Running phc gives
<?php
function bar()
{
return 5;
}
$foo = bar();
echo "foo is " . $foo . "<br>";
?>
where the name of the function has been changed, while the name of the
variable remained unaltered, as has the text "foo" inside
the string. It's that simple! Of course, in this example, it would
have been quicker to do it by hand, but that's not the point; the
example shows how easy it is to operate on PHP scripts within the
phc framework.
|