Last tested using Eclipse 4.3.2 (Kepler), PDT v3.3.0
Eclipse PDT's 'PHP Editor Templates' allow us to create custom autocompletion templates.
This is a small collection of templates I have created for personal use. These templates create code blocks that allow me to:
- View the contents of variables and objects
- Keep track of where I am in my code
- Quickly add 'return true/false;' statements while creating method stubs
- Save the 'eclipse_php_editor_helper_templates.xml' file to your hard drive (found below)
- Launch Eclipse
- Open the 'Preferences' window: 'Window -> Preferences'
- Go into the 'PHP' tree branch
- Go into the 'Editor' tree branch
- Click the 'Templates' tree item
- Click the 'Import' button located on the left side of the window
- Navagate to the 'eclipse_php_editor_helper_templates.xml' file on your hard drive, select it, then click 'Ok'
Sequence:
type_this_and_press_enter
Resulting Block:
<p>Block of code generated</p>
Example:
A usage example goes here if needed.
Create an echo statement that will display the current file name and line number:
Sequence:
varl (that's a lowercase L)
Resulting Block:
//*
echo '<br />I am here: ' . __FILE__ . ': ' . __LINE__ . '<br />';
//exit;
//*/
Create an echo statement to view the value of scalar types:
Sequence:
vare
Resulting Block:
//*
echo '<br /> is: ' . . '<br />';
//exit;
//*/
Example:
$var1 = 'Hello World!';
//*
echo '<br />$var1 is: ' . $var1 . '<br />';
//exit;
//*/
Create a print_r block to view the contents of compound types:
Sequence:
varp
Resulting Block:
//*
echo '<br /><pre>: ';
echo print_r();
echo '</pre><br />';
//exit;
//*/
Example:
$array1 = array(1, 2, 3);
//*
echo '<br /><pre>$array1: ';
echo print_r($array1);
echo '</pre><br />';
//exit;
//*/
Create a var_dump block to view the contents of compound types. Also a great way to verify the value of a bool:
Sequence:
vard
Resulting Block:
//*
echo '<br /><pre>: ';
echo var_dump();
echo '</pre><br />';
//exit;
//*/
Example:
$object1 = new SomeClass();
//*
echo '<br /><pre>$object1: ';
echo var_dump($object1);
echo '</pre><br />';
//exit;
//*/
Create a return true statement, often used when creating method stubs:
Sequence:
rett
Resulting Block:
return true;
Create a return false statement, often used when creating method stubs:
Sequence:
retf
Resulting Block:
return false;
Toggling the blocks (commenting/uncommenting them out)
Other than the 'return' templates you will notice that the first line of the block contains this:
//*
If you remove one of the forward slashes in the first line of the block the block will be commented out:
/*
This is handy if you don't want the block to execute momentarily, yet you have not finished testing with the block.
Terminating script execution at the end of the block
Other than the 'return' templates you'll notice that all of the blocks contain a commented out 'exit' statement:
//exit;
Sometimes you might want to make sure the value of a variable or object is what you expect it to be, but you don't want to continue script execution past your testing block. Just uncomment the 'exit' statement and script execution will be terminated:
exit;