开发者

PHP callback vs template

开发者 https://www.devze.com 2023-03-22 06:18 出处:网络
I\'ve built a list rendering class: class ListRenderer { /** 开发者_开发百科* @param int $columns number of columns

I've built a list rendering class:

class ListRenderer
{
    /**
 开发者_开发百科    * @param int $columns number of columns
     * @param string $element container element
     * @param string $styleClass container style
     */
    public function __construct($columns,$element='div',$styleClass=''){...}
    ...
    /**
     * @param mixed $callback function to render items - should take two
     *        parameters ($item,$index)
     * @param array $list items to render
     */
    public function renderArrayList($callback,$list){...}

    /**
     * @param mixed $callback function to render items - should take 3 parameters
     *        ($row,$i,$count) $i and $count are the position and total items
     * @param string $sql query string
     * @param string $errorMessage
     * @param int $blanks number of blank items to render. The callback will be
     *        invoked with a null $row parameter for the blank records.
     */
    public function renderQueryList($callback,$sql,$errorMessage,$blanks=0){...}
    ...
}

The callback function renders a single item.

This could also be accomplished using templates:

class ListRenderer
{
    ...
    //$itemRenderer implements ListItemRenderer
    public function renderArrayList($itemRenderer,$list){...}
    //$itemRenderer implements ListItemRenderer
    public function renderQueryList($itemRenderer,$sql,$errorMessage,$blanks=0){...}
    ...
}

template ListItemRenderer
{
    public function renderArrayItem($item,$index);
    public function renderQueryItem($row,$index,$count);
}

class SomeClass implements ListItemRenderer
{
    ...
    public function renderArrayItem($item,$index){...}
    public function renderQueryItem($row,$index,$count){...}
    ...
}

I'm not sure why I went with callbacks on this one; coming from a Java background I would normally be inclined to use the second approach.

It seems to me that:

  • Callbacks are more flexible
    • Templates would limit a single class to one renderArrayItem function, for example, where callbacks would allow use of multiple functions per class for that purpose.
    • The template approach requires the function to be a class member.
  • Callbacks tend to produce less maintainable code.

Are there any strong reasons to go one way or the other on this?


There can be multiple reasons for the one versus the other and the other way round. Especially for your case I have no clue what the difference is because I don't know your application.

So I ask back: Why one versus the other? If you still don't know which way to go, or unsure if you want the one or the other explicitly, why don't you make a callback variant you can use when needed? You can inject the callbacks when instantiating the class:

class ListItemCallbackRenderer implements ListItemRenderer
{
    private $callbacks;
    public function __construct(array $callbacks)
    {
        $this->callbacks = $callbacks;
    }
    public function renderArrayItem($item,$index)
    {
        $callback = $this->callbacks[__FUNCTION__];
        // ...
    }
    public function renderQueryItem($row,$index,$count)
    {
        $callback = $this->callbacks[__FUNCTION__];
        // ...
    }
}

Done this, the interface stays the same which makes your overall design more fluid and you can decide which variant to use everywhere in your application. No need to degrade yourself to one method actually.

0

精彩评论

暂无评论...
验证码 换一张
取 消