ZF-11730: Incorrect method name in title - Using the static staticFilter() method


The title of the section "Using the static staticFilter() method" is incorrect as the actual method discussed is filterStatic(). I'd argue minor rather than trivial as some people may skim through the documentation for the name of a method they cannot remember and not read the entire paragraph.

Also, and this may be a different issue, why does it need a different name from the non-static method? Can the method not be written to work out if it is being called from an instantiated class or not - ala singleton pattern and either behave as static or not as needed? I can't see any reason for the 'static' naming convention in the ZF coding standards.


Commenting only on your second paragraph.

In PHP4, it was possible to call a method both statically and as an instance method, and test for $this. In PHP5, the only way for a method to be static is to declare it as such from the outset. Additionally, when calling a static method from an object instance, $this remains unset. As an example, try the following:

class Foo                                                                       
    protected $seed;                                                            
    public function __construct($seed)                                          
        $this->seed = $seed;                                                    
    public static function filter($value)                                       
        if (!isset($this)) {                                                    
            return strtoupper($value);                                          
        return strtoupper($this->seed . ' ' . $value);                          
$f = new Foo('I say');                                                          
echo $f->filter('boo');

The result will still be "BOO". As such, you lose access to any state you might have set in the object.

Because many filters may depend on state (e.g. locale settings, configuration that alters behavior, etc.), Zend_Filter_Interface thus defines filter() as a public, non-static method. Even for filters that do not need state, in order to adhere to the interface, they need to declare the method non-static.

Finally, the Zend_Filter class itself serves two purposes. One is to provide filter chain capabilities -- and that is what the filter() method of that class is for (it iterates through all attached filters, passing the return value of one to the next until done).

The second purposes is where filterStatic() comes in; its purpose is for doing one-off filtering on a single line:

$filtered = Zend_Filter::filterStatic('boo', 'StringToUpper', array('encoding' => 'utf-8'));
echo $filtered; // 'BOO'

As such, the difference in naming is by design.

Thanks for the detail - makes total sense. I was thinking in PHP4 terms where you declare filter() as a non-static method then the following works as expected and is perfectly valid (according to PHP4 docs on paamayim-nekudotayim, but not PHP5 docs):

echo Foo::filter('boo');
<?php $f = new Foo('I say'); echo $f->filter('boo');

FYI for anyone that tries this in PHP5 - calling a non-static method statically still works (according to various php bug reports this behaviour will be kept in for backward compatibility purposes) but certainly isn't good practice - sure enough it throws up a 'php Non-static method method() should not be called statically on line #' E_STRICT warning. Thinking about it - it seems odd this was possible with PHP4 in the first place as it isn't in any other OOP language I am familiar with. The fact that you can access a $this (from parent) within a static method in PHP5 if the class is inherited only adds to the confusion.

Fixed in ZF2 with GH-414