Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Inflection Rules

As mentioned in the introduction, there are two types of rules: static and filter-based.


It is important to note that regardless of the method in which you add rules to the inflector, either one-by-one, or all-at-once; the order is very important. More specific names, or names that might contain other rule names, must be added before least specific names. For example, assuming the two rule names 'moduleDir' and 'module', the 'moduleDir' rule should appear before module since 'module' is contained within 'moduleDir'. If 'module' were added before 'moduleDir', 'module' will match part of 'moduleDir' and process it leaving 'Dir' inside of the target uninflected.

Static Rules

Static rules do simple string substitution; use them when you have a segment in the target that will typically be static, but which you want to allow the developer to modify. Use the setStaticRule() method to set or modify the rule:

= new Zend_Filter_Inflector(':script.:suffix');

// change it later:

Much like the target itself, you can also bind a static rule to a reference, allowing you to update a single variable instead of require a method call; this is often useful when your class uses an inflector internally, and you don't want your users to need to fetch the inflector in order to update it. The setStaticRuleReference() method is used to accomplish this:

class Foo
     * @var string Suffix
protected $_suffix 'phtml';

     * Constructor
     * @return void
public function __construct()
$this->_inflector = new Zend_Filter_Inflector(':script.:suffix');

     * Set suffix; updates suffix static rule in inflector
     * @param  string $suffix
     * @return Foo
public function setSuffix($suffix)
$this->_suffix $suffix;

Filter Inflector Rules

Zend_Filter filters may be used as inflector rules as well. Just like static rules, these are bound to a target variable; unlike static rules, you may define multiple filters to use when inflecting. These filters are processed in order, so be careful to register them in an order that makes sense for the data you receive.

Rules may be added using setFilterRule() (which overwrites any previous rules for that variable) or addFilterRule() (which appends the new rule to any existing rule for that variable). Filters are specified in one of the following ways:

  • String. The string may be a filter class name, or a class name segment minus any prefix set in the inflector's plugin loader (by default, minus the 'Zend_Filter' prefix).

  • Filter object. Any object instance implementing Zend_Filter_Interface may be passed as a filter.

  • Array. An array of one or more strings or filter objects as defined above.

= new Zend_Filter_Inflector(':script.:suffix');

// Set rule to use Zend_Filter_Word_CamelCaseToDash filter

// Add rule to lowercase string
$inflector->addFilterRule('script', new Zend_Filter_StringToLower());

// Set rules en-masse
$inflector->setFilterRule('script', array(

Setting Many Rules At Once

Typically, it's easier to set many rules at once than to configure a single variable and its inflection rules at a time. Zend_Filter_Inflector's addRules() and setRules() method allow this.

Each method takes an array of variable and rule pairs, where the rule may be whatever the type of rule accepts (string, filter object, or array). Variable names accept a special notation to allow setting static rules and filter rules, according to the following notation:

  • ':' prefix: filter rules.

  • No prefix: static rule.

Example 439. Setting Multiple Rules at Once

// Could also use setRules() with this notation:
// filter rules:
':controller' => array('CamelCaseToUnderscore','StringToLower'),
':action'     => array('CamelCaseToUnderscore','StringToLower'),

// Static rule:
'suffix'      => 'phtml'

Zend Framework