Generate PHP codes with PHP

Throughout my programming years, I observed similar trends in code structures and I felt that being able to generate them automatically will greatly reduce development time.

Although there are many code generation software on the web, coding one for your own usage can be simpler and more convenient, especially when you had written custom functions that your generated codes will utilize.

In this post, I shall discuss my recent experience in creating a PHP code generator for my own usage.

A simple case study

Suppose that given a class name and a comma-delimited string of variable names:

$classname = 'Person';
$variables = 'age, name';

We want to generate a POJO equivalence in PHP automatically, which resembles the following form:

class Person {

    private $name;
    private $age;

    public function getName() {
        return $this->name;
    } // end public function getName()

    public function setName($name) {
        $this->name = $name;
    } // end public function setName($name)

    public function getAge() {
        return $this->age;
    } // end public function getAge()

    public function setAge($age) {
        $this->age = $age;
    } // end public function setAge($age)
} // end class Person


Perform the necessary work

Capitalise the first character of the variable name

To capitalise the first character of the variable name to form the getXXX and setXXX methods, we can use the ucfirst function.

// Form the method name of the getter method 
$getterMethodName = 'get' . ucfirst('age');

Get the end of line

The end of line is represented differently in different operating systems. Use the php constant PHP_EOL instead of \r or \n to output the end of line in a cross platform manner.

// Variable contains the end of line character(s).
$endOfLine = PHP_EOL;

Get the tab character

The tab character is represented with the \t literal. To output the tab character, enclose it with double quotes instead of single quotes. Using single quotes will display two characters \ and t instead of the tab character.

// Variable contains the tab character
$tab = "\t";
// Variable contains the \ and t characters
$slashAndT = '\t';

Write the codes to file

To write the codes to file, we can use the fopen and fwrite functions.

// Save the generated file to a sibling folder of the running script
$generatedFileUrl  = dirname($_SERVER['SCRIPT_FILENAME']);
$generatedFileUrl .= '/generated-codes/' . $className . '.inc.php';
$fp = fopen($generatedFileUrl, 'w+');
// Write the start script tag
fwrite($fp, '<?php' . PHP_EOL);

A sample (Plain Old PHP Object) POPO code generation script

The following is a sample script that I wrote for generating POPOs using the above-mentioned PHP features. Since this is a script to help developers, I did not include error handling to filter invalid class names or variable names. If you use it the right way, it can be helpful to you. 🙂


$className = '';
$variableNames = '';

if (isset($_POST['text_class_name']) && isset($_POST['text_variable_names'])) {
    $className = trim($_POST['text_class_name']);
    $variableNames = str_replace(' ', '', trim($_POST['text_variable_names']));

    $variableNamesArray = explode(',', $variableNames);
    // Save the generated file to a sibling folder of the running script
    $generatedFileUrl  = dirname($_SERVER['SCRIPT_FILENAME']);
    $generatedFileUrl .= '/generated-codes/' . $className . '.inc.php';
    $fp = fopen($generatedFileUrl, 'w+');
    // Write the start script tag
    fwrite($fp, '<?php' . PHP_EOL);
    fwrite($fp, 'class ' . $className . ' {' . PHP_EOL . PHP_EOL);
    // Output the instance variables
    foreach($variableNamesArray as $variableName) {
        fwrite($fp, "\t" . 'private $' . $variableName . ';' . PHP_EOL);
    fwrite($fp, PHP_EOL);
    // Output the getter and setter methods
    foreach ($variableNamesArray as $variableName) {
        $ucVariableName = ucfirst($variableName);
        // getter 
        fwrite($fp, "\tpublic function get" . $ucVariableName . '() {' . PHP_EOL);
        fwrite($fp, "\t\t" . 'return $this->' . $variableName . ';' . PHP_EOL );
        fwrite($fp, "\t} // end function get" . $ucVariableName . '()' . PHP_EOL . PHP_EOL);
        // setter 
        fwrite($fp, "\tpublic function set" . $ucVariableName . '($' . $variableName . ') {' . PHP_EOL);
        fwrite($fp, "\t\t" . '$this->' . $variableName . ' = $' . $variableName . ';' .  PHP_EOL);
        fwrite($fp, "\t} // end function set" . $ucVariableName . '($' . $variableName . ')' . PHP_EOL . PHP_EOL);
    } // end foreach
    fwrite($fp, '} // end class ' . $className . PHP_EOL);
    fwrite($fp, '?>');
} // end if

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="en" lang="en">
    <title>Create Plain Old PHP Object</title>
<h1>Create Plain Old PHP Object</h1>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"];?>">
<label>Class name: </label>
<input type="text" name="text_class_name" value="<?php echo $className;?>"/>
<label>Variable name(s), comma-delimited: </label>
<input type="text" name="text_variable_names" value="<?php echo $variableNames;?>"/>
<input type="submit" id="submitButton" name="submitButton" value="Submit"/>

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns and hopes that whatever he had written and built so far had benefited people.