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

  • SEARCH
  • Home
  • 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 8146241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:55:49+00:00 2026-06-06T13:55:49+00:00

I’m working on a web-accessible API and have been trying to figure out the

  • 0

I’m working on a web-accessible API and have been trying to figure out the tidiest and most efficient way of assigning $_POST / $_GET variables as function parameters. I know variables can’t be used as parameter defaults, so the goal is DRY (Don’t Repeat Yourself); to have API functions usable from both within the application and through the external interface.

Background:

1) Currently, API functions are organised into classes with public static methods. When a remote request is made, the requested class and method are (safely) determined, and if the class+method combination is valid, it is called using call_user_func("$class::$method").

2) The the code that ends up calling class::method() is intentionally dumb – it doesn’t know or care about the parameters a function takes. As functions cannot be called with missing parameters, all functions in the API must either have no parameters, or no parameters without defaults. I figured the cleanest thing to do here was to have required parameters defined with a default of null and let functions handle the assignment of $_POST variables internally:

public static function complete($id = null, $offset = 0, $limit = 50){ ... }

3) Internally, API methods return regular PHP variables, then the external interface encodes this value with json_encode().

What I have now that works:

I tried a few monstrosities at first that required 10 or more extra lines per function, but I’ve whittled it down to a reusable function. I had a look at extract() but it wasn’t precise or safe enough for my purpose.

function assign($postIndex, &$target, $require = true){
    // Any code assigned parameter overrides remote values
    if($target != null && $require == true)
        return;

    // Force parameter requirement if specified
    if($require && !isset($_POST[$postIndex]))
        throw new MissingParameterException();

    // Assign to referenced variable if it's set
    if(isset($_POST[$postIndex]))
        $target = $_POST[$postIndex];
}

Used like:

public static function delete($id = null){
    assign('id', $id);

    ...

}

Please note that I am aware of the potential injection vulnerability; all API functions clean their parameters regardless of origin. I am also aware that other people writing functions for an API using this method might assume parameters are safe, however as assign() has to be explicitly called, I think this is somewhat void (more so if the function was renamed assign_raw_post_var_to_param()) – it’s essentially as explicit as writing $name = $_POST['name'];.

The question…

Is there a better way to assign $_POST / $_GET variables to function parameters that allows functions to be called both through an external interface and from code transparently? How would you implement it if so?

  • 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-06-06T13:55:51+00:00Added an answer on June 6, 2026 at 1:55 pm

    I think you need call_user_func_array. Using that you can specify an array of parameters so you can just keep plain method signatures.

    If you don’t know the order of parameters and need to figure if out run-time, you could do so using reflections.


    Edit by OP:

    Reflection is an excellent solution. For example:

    class TestClass {
        public static function some_method($name, $email, $bannana){
    
        }
    }
    
    $reflection = new ReflectionMethod('TestClass', 'some_method');
    $params = $reflection->getParameters();
    

    $params now looks like this:

    Array
    (
        [0] => ReflectionParameter Object
            (
                [name] => name
            )
    
        [1] => ReflectionParameter Object
            (
                [name] => email
            )
    
        [2] => ReflectionParameter Object
            (
                [name] => bannana
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.