Skip to content

Instantly share code, notes, and snippets.

@jburns131
Last active August 29, 2015 14:03
Show Gist options
  • Save jburns131/6e39f6856ca3456a7f73 to your computer and use it in GitHub Desktop.
Save jburns131/6e39f6856ca3456a7f73 to your computer and use it in GitHub Desktop.
Eclipse PHP Editor DebuggingTemplates - Quick and easy code blocks that help speed up debugging time
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="php" deleted="false" description="Create a return false statement, often used when creating method stubs." enabled="true" name="retf">return false;</template><template autoinsert="true" context="php" deleted="false" description="Create a return true statement, often used when creating method stubs." enabled="true" name="rett">return true;</template><template autoinsert="true" context="php" deleted="false" description="Create a var_dump block to view the contents of compound types. Also a great way to verify the value of a bool." enabled="true" name="vard">//*&#13;
echo '&lt;br /&gt;&lt;pre&gt;: ';&#13;
echo var_dump();&#13;
echo '&lt;/pre&gt;&lt;br /&gt;';&#13;
//exit;&#13;
//*/</template><template autoinsert="true" context="php" deleted="false" description="Create an echo statement to view the value of scalar types." enabled="true" name="vare">//*&#13;
echo '&lt;br /&gt; is: ' . . '&lt;br /&gt;';&#13;
//exit;&#13;
//*/</template><template autoinsert="true" context="php" deleted="false" description="Create an echo statement that will display the current file name and line number." enabled="true" name="varl">//*
echo '&lt;br /&gt;I am here: ' . __FILE__ . ': ' . __LINE__ . '&lt;br /&gt;';
//exit;
//*/</template><template autoinsert="true" context="php" deleted="false" description="Create a print_r block to view the contents of compound types." enabled="true" name="varp">//*&#13;
echo '&lt;br /&gt;&lt;pre&gt;: ';&#13;
echo print_r();&#13;
echo '&lt;/pre&gt;&lt;br /&gt;';&#13;
//exit;&#13;
//*/</template></templates>

Front Matter

Last tested using Eclipse 4.3.2 (Kepler), PDT v3.3.0

Explanation

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

Installation

  1. Save the 'eclipse_php_editor_helper_templates.xml' file to your hard drive (found below)
  2. Launch Eclipse
  3. Open the 'Preferences' window: 'Window -> Preferences'
  4. Go into the 'PHP' tree branch
  5. Go into the 'Editor' tree branch
  6. Click the 'Templates' tree item
  7. Click the 'Import' button located on the left side of the window
  8. Navagate to the 'eclipse_php_editor_helper_templates.xml' file on your hard drive, select it, then click 'Ok'

Basic Usage

Key

Sequence:

type_this_and_press_enter

Resulting Block:

<p>Block of code generated</p>

Example:

A usage example goes here if needed.

Sequences

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;

Advanced Usage

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment