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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:27:07+00:00 2026-05-12T11:27:07+00:00

I have a best practice question. I’m developing a OO MVC framework in PHP,

  • 0

I have a “best practice” question. I’m developing a OO MVC framework in PHP, and most of the classes interact easily – they are literally declared in the code and used. For instance:

// In class 'getDetails'
$db = new mysqli(.....);
$db->query(.....);

However, there are times when the class and function names are dynamically built. The actual class files are all created and located in the framework somewhere, but they are not all literally declared and used. It isn’t until run time that the framework knows what class it needs to complete the request; so the class and function names are usually created and stored in variables. In the simplest case, the variables are used to create an object and run a function. Example:

$request = 'blog'; 
$action = 'view';
$class = new $request(); // Creates an blog object
$class->$action(); // Runs the blog function view

However, I have already run into a situation when trying to use variables to run static functions (here is the stack overflow question and answer) where the variables couldn’t be used in the literal usage
( $request::$action() gives parse errors). I have seen in the PHP manual functions for interacting/using classes, functions, and objects, but have not had to deal with them before.

My question is what is the best way to handle and run classes and functions where the class and function names are created on the fly?

  • 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-12T11:27:07+00:00Added an answer on May 12, 2026 at 11:27 am

    Both methods you mentioned are both good but have some limitations:

    Using the regular notation:

    $request = 'blog'; 
    $action = 'view';
    $class = new $request(); // Creates an blog object
    $class->$action(); // Runs the blog function view
    

    Using the notation for instantiating classes requires that you know in advance the parameters each class/method accepts. So you cannot design a factory pattern with it that will accept arbitrary parameters.

    Using call_user_func_array() allows you to use arbitrary parameters.

    $request = 'blog'; 
    $action = 'view';
    $params = array(
       $_GET['category'],
       $_GET['limit']
    );
    call_user_func_array(array($request, $action), $params);
    

    So the above code is equivalent to the literal:

    blog::view($_GET['category'], $_GET['limit']);
    

    Basically, call_user_func_array() flattens the array $params, passing each value in it as parameters to the method blog::view().

    To do the same with dynamic/object method call:

    call_user_func_array(array(new $request, $action), $params);
    

    However, this does not solve the problem with creating an instance of an arbitrary class, and passing it an arbitrary number of parameters. To do this you can use the ReflectionClass.

    Example:

    $request = 'blog';
    $action = 'view';
    $configs = array('something', 'something else');
    $params = array(
       $_GET['category'],
       $_GET['limit']
    );
    $instance = call_user_func_array(
       array(new ReflectionClass($request), 'newInstance'), 
       $configs
    );
    $return = call_user_func_array(array($instance, $action), $params);
    

    This would be equivalent to:

    $configs = array('something', 'something else');
    $params = array(
       $_GET['category'],
       $_GET['limit']
    );
    $blog = new blog($configs[0], $configs[1]);
    $blog->view($_GET['category'], $_GET['limit']);
    

    With those tools you can dynamically instantiate arbitrary Objects and pass arbitrary number of parameters to their __constructor() as well as any method.

    If you meant best in terms of functionality, use call_user_func_array() and ReflectionClass().
    If you meant best in terms of performance, don’t worry about it. Good design and functionality improves performance more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like you'd want this to be a Web… May 13, 2026 at 2:03 am
  • Editorial Team
    Editorial Team added an answer use "live" to bind events for dynamically created elements. You… May 13, 2026 at 2:03 am
  • Editorial Team
    Editorial Team added an answer Using regex is not the correct way to do this.… May 13, 2026 at 2:03 am

Related Questions

I have a best practice question. I'm developing a OO MVC framework in PHP,
I just have a quick best practice question regarding custom cells in a UITableView.
I have a best practice type of question regarding Nibs and UISegmentedControls. I have
I have a class Employee. I want to be able to Validate() it before
This is a very generic 'best practice' question, but here's an example. Let's say

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.