Issues

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

Issue Type: Docs: Problem Created: 2011-09-09T13:58:12.000+0000 Last Updated: 2011-09-15T08:24:50.000+0000 Status: Resolved Fix version(s): - Next Major Release ()

Reporter: Nick (uberteck) Assignee: Thomas Weidner (thomas) Tags: - Zend_Filter

Related issues: Attachments:

Description

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.

Comments

Posted by Matthew Weier O'Phinney (matthew) on 2011-09-09T14:16:09.000+0000

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:

<pre class="highlight">
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:

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

As such, the difference in naming is by design.

Posted by Nick (uberteck) on 2011-09-10T09:57:17.000+0000

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):

<pre class="highlight">
echo Foo::filter('boo');
?><br></br><?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.

Posted by Thomas Weidner (thomas) on 2011-09-15T08:24:50.000+0000

Fixed in ZF2 with GH-414

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts