Previous Up Next
Introduction to PHP_Beautifier Introduction to PHP_Beautifier How to use PHP_Beautifier

Callbacks

Describe how the file to beautify can alter the beautify process

Claudio Bustos cdx@users.sourceforge.com Jens Bierkandt scortch@users.sourceforge.com

Table of Contents

Introduction

One of the nicest features of Php_Beautifier is the use of callbacks inside the code to beautify. This callbacks call a method on the base class or in a filter.

Usage

To execute a callback, you should insert a short comment, with this structure

  • Php_Beautifier or the name of the filter (case insensitive)

  • ->

  • Method name and params inside parenthesis. Php_Beautifier evaluates all after '->', so the code must be valid PHP code

  1. // php_beautifier->setBeautify(true);
  2. echo "This line will be beautified";
  3. // ArrayNested->on()
  4. echo "Array Nested!";

Common methods

Base class

You can use only the public method that starts with 'add' and 'set', except PHP_Beautifier::setInputFile() and PHP_Beautifier::setOutputFile()

Filters

Demostration

With this code to beautify

  1. <?php
  2. $a=array('1',array('1.1','1.2','1.3'));
  3. // php_beautifier->addFilter('ArrayNested');
  4. $a=array('1',array('1.1','1.2','1.3'));
  5. // php_beautifier->setIndentNumber(2);
  6. echo 'Indent: 2 spaces';
  7. // php_beautifier->setIndentNumber(10);
  8. echo 'Indent: 10 spaces';
  9. // php_beautifier->setBeautify(false);
  10. echo "The following lines won't be beautified";
  11. $a=array('1',array('1.1','1.2','1.3'));
  12. if ($b{$c;else {$d;}
  13. // php_beautifier->setBeautify(true);
  14. // php_beautifier->setIndentNumber(4);
  15. echo 'Beautify, again';
  16. // now, turn out the Filter
  17. // ArrayNested->off()
  18. $a=array('1',array('1.1','1.2','1.3'));
  19. // ArrayNested->on()
  20. $a=array('1',array('1.1','1.2','1.3'));
  21. ?>

With the defaults settings we obtain

  1. <?php
  2.     $a array('1'array('1.1''1.2''1.3'));
  3.     // php_beautifier->addFilter('ArrayNested');
  4.     $a array(
  5.         '1',
  6.         array(
  7.             '1.1',
  8.             '1.2',
  9.             '1.3'
  10.         )
  11.     );
  12.     // php_beautifier->setIndentNumber(2);
  13.   echo 'Indent: 2 spaces';
  14.   // php_beautifier->setIndentNumber(10);
  15.           echo 'Indent: 10 spaces';
  16.           // php_beautifier->setBeautify(false);
  17. echo "The following lines won't be beautified";
  18. $a=array('1',array('1.1','1.2','1.3'));
  19. if ($b{$c;else {$d;}
  20. // php_beautifier->setBeautify(true);
  21.           // php_beautifier->setIndentNumber(4);
  22.     echo 'Beautify, again';
  23.     // now, turn out the Filter
  24.     // ArrayNested->off()
  25.     $a array('1'array('1.1''1.2''1.3'));
  26.     // ArrayNested->on()
  27.     $a array(
  28.         '1',
  29.         array(
  30.             '1.1',
  31.             '1.2',
  32.             '1.3'
  33.         )
  34.     );
  35. ?>

More information

See PHP_Beautifier::controlToken()

Previous Up Next
Introduction to PHP_Beautifier Introduction to PHP_Beautifier How to use PHP_Beautifier

Documentation generated on Mon, 31 May 2010 16:16:02 -0400 by phpDocumentor 1.4.3