Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:16:18+00:00 2026-05-24T01:16:18+00:00

I’ve built a list rendering class: class ListRenderer { /** * @param int $columns

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:16:18+00:00Added an answer on May 24, 2026 at 1:16 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.