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 6189487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:25:22+00:00 2026-05-24T02:25:22+00:00

When writing a CRUD MVC application, would you suggest using arrays instead of long

  • 0

When writing a CRUD MVC application, would you suggest using arrays instead of long (even short) parameter lists when writing an api for your business layer (model)?

For instance, which would you suggest:

1

// Posts::getPosts(20, 0, $category, 'date_added');
static function getPosts($limit = NULL, $offset = NULL, Model_Category $category = NULL, $sort_by = NULL);

2

// Posts::getPosts(array('limit' => 20, 'offset' => 0, 'category' => $category, 'sort_by' => 'date_added'));
static function getPosts(array $options = NULL);`

1 seems a lot cleaner and less prone to bugs, but 2 seems WAY more flexible (can easily add/switch parameters without changing the api). Just looking for reasons to go either way.

Thanks

  • 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-24T02:25:23+00:00Added an answer on May 24, 2026 at 2:25 am

    I go with this rule of thumb:

    If

    • there are more than 5 arguments
    • there is no logical order of the arguments
    • there is not a clear logical dependency between arguments
      and/or
    • most of the arguments are optional

    Then it is probably a good idea to use an array to simulate keyword arguments. Otherwise, just go with standard arguments.

    Also, consider using a parameter object to do complicated method calls.

    EDIT: What would I do with this?

    public static function search(
      $keywords,
      $limit = NULL,
      $offset = NULL,
      Model_Post_Type $type = NULL,
      Model_Category $category = NULL
    )
    

    Well, with a parameter array (also known as keyword arguments in languages that support them, like Python), my personal preference would be to do this:

    public static function search($keywords, $options = array()) {
      $default_options = array(
        'limit' => NULL,
        'offset' => NULL,
        'post_type' =>  NULL,
        'category' => NULL
      );
      extract(array_merge($default_options,$options));
      // search logic, using $keywords, $limit, $offset, $post_type, $category
    }
    

    This gives you a few benefits:

    • Anything in $options is completely optional. Any required arguments should be arguments.
    • Gives you complete control over the defaults of those options, even allowing for complicated expressions in the array initializer.
    • Allows you to add new (optional) search options later on, while remaining backwards-compatible with existing code.
    • extract() makes key-value pairs into variable-value pairs, so the rest of the method is completely oblivious to the fact you’re using a parameter array and not normal arguments.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between
I am currently writing a CRUD class in PHP using PDO. I like the
I am writing an application that's sole purpose in life is to do CRUD
Here's the situation: I'm writing a simple AJAX application that performs CRUD functions. When
I am writing a CRUD using winforms, connecting to MS SqlServer 2008 via Linq2Sql.
I am writing an application to CRUD user details. I kick off the page
I am writing an EF4 datalayer for an MVC 2 web application and I
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing my first Linq application, and I'm trying to find the best way to
Writing a python program, and I came up with this error while using the

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.