Пример #1
0
 public function testExample()
 {
     $generator = new ezcPhpGenerator(dirname(__FILE__) . '/data/fibo.php', true, true);
     $generator->appendCustomCode('function fibonacci( $number )');
     $generator->appendCustomCode("{");
     $generator->appendValueAssignment("lo", 0);
     $generator->appendValueAssignment("hi", 1);
     $generator->appendValueAssignment("i", 2);
     $generator->appendWhile('$i < $number');
     $generator->appendCustomCode('$hi = $lo + $hi;');
     $generator->appendCustomCode('$lo = $hi - $lo;');
     $generator->appendCustomCode('$i++;');
     $generator->appendEndWhile();
     $generator->appendCustomCode('return $hi;');
     $generator->appendCustomCode("}");
     $generator->finish();
     require dirname(__FILE__) . '/data/fibo.php';
     $this->assertEquals(34, fibonacci(10));
     unlink(dirname(__FILE__) . '/data/fibo.php');
 }
Пример #2
0
<?php

/**
 * File containing example of using ezcPhpGenerator
 * @package PhpGenerator
 */
$generator = new ezcPhpGenerator("~/file.php");
$generator->appendCodePiece("function fibonacci( {$number} )");
$generator->appendCodePiece("{");
$generator->appendVariable("lo", 1);
$generator->appendVariable("hi", 1);
$generator->appendVariable("i", 2);
$generator->appendWhile('$i < $number');
$generator->appendVariable("hi", "{$lo} + {$hi}");
$generator->appendVariable("lo", "{$hi} - {$lo}");
$generator->appendEndWhile();
$generator->appendCodePiece("}");
?>

The above code will fill the file "~/file.php" with the following contents:

<?php 
/**
 * function fibonacci
 */
function fibonacci($number)
{
    $lo = 1;
    $hi = 1;
    $i = 2;
    while ($i < $number) {