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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:31:51+00:00 2026-05-25T20:31:51+00:00

I’m writing an API class, and my general goal is for it to be

  • 0

I’m writing an API class, and my general goal is for it to be easy to make any class’s methods accessible via the API, without having to make any serious changes to the class itself. Essentially, I should be able to instantiate an API class instance on any class that I want to use (within my little framework), and have it just work.

For example, In my API class, I have a method call, that I want to use $_GET to call the correct function from the class that I want to make accessible (let’s call it Beep). So I specify an action parameter in my API, so that the action is the method of Beep to call, with the remaining arguments in $_GET being, presumably, the arguments for the method. In API->call, I can do $BeepInstance->$_GET['action'](), but I have no way of determining which arguments from $_GET to send, and in what order to send them.

func_get_args will only return the list of given arguments for the function in which it is called, and I don’t necessarily know the correct order in which to pass them with call_user_func_array.

Has anyone tried to do something similar to 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-25T20:31:51+00:00Added an answer on May 25, 2026 at 8:31 pm

    Here’s a solution + example that uses reflection to map your input arguments to method parameters. I also added a way to control which methods are exposed to make it more secure.

    class Dispatch {
        private $apis;
    
        public function registerAPI($api, $name, $exposedActions) {
            $this->apis[$name] = array(
                'api' => $api,
                'exposedActions' => $exposedActions
            );
        }
    
        public function handleRequest($apiName, $action, $arguments) {
            if (isset($this->apis[$apiName])) {
                $api = $this->apis[$apiName]['api'];
                // check that the action is exposed
                if (in_array($action, $this->apis[$apiName]['exposedActions'])) {
                    // execute action
    
                    // get method reflection & parameters
                    $reflection = new ReflectionClass($api);
                    $method = $reflection->getMethod($action);
    
                    // map $arguments to $orderedArguments for the function
                    $orderedArguments = array();
    
                    foreach ($method->getParameters() as $parameter) {
                        if (array_key_exists($parameter->name, $arguments)) {
                            $orderedArguments[] = $arguments[$parameter->name];
                        } else if ($parameter->isOptional()) {
                            $orderedArguments[] = $parameter->getDefaultValue();
                        } else {
                            throw new InvalidArgumentException("Parameter {$parameter->name} is required");
                        }
                    }
    
                    // call method with ordered arguments
                    return call_user_func_array(array($api, $action), $orderedArguments);
                } else {
                    throw new InvalidArgumentException("Action {$action} is not exposed");
                }
            } else {
                throw new InvalidArgumentException("API {$apiName} is not registered");
            }
        }
    }
    
    class Beep {
        public function doBeep($tone = 15000)
        {
            echo 'beep at ' . $tone;
        }
    
        public function notExposedInAPI()
        {
            // do secret stuff
        }
    }
    

    Example:

    // dispatch.php?api=beep&action=doBeep&tone=20000
    
    $beep = new Beep();
    $dispatch = new Dispatch();
    $dispatch->registerAPI($beep, 'beep', array('doBeep'));
    
    $dispatch->handleRequest($_GET['api'], $_GET['action'], $_GET);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
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

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.