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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:07:39+00:00 2026-06-10T11:07:39+00:00

I need to be able to call a method without having to know whether

  • 0

I need to be able to call a method without having to know whether or not it is static.

For example, this doesn’t work and I would like it to:

class Groups {
    public function fetchAll($arg1, $arg2){
        return $this->otherFunction();
    }
    public static function alsoFetchAll($arg1, $arg2){}
}

$arguments = array('one', 'two');
$result = call_user_func_array(array('Groups', 'fetchAll'), $arguments);
$result = call_user_func_array(array('Groups', 'alsoFetchAll'), $arguments);

I’m getting an error for the instance varirable:

Fatal error: Using $this when not in object context

The reason it doesn’t work is because I need to instantiate the class for the instance variables to work. But my constructor doesn’t take any arguments so I want a quick way to skip this step.

How can I write this so that it doesn’t matter what kind of method it is?

  • 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-10T11:07:41+00:00Added an answer on June 10, 2026 at 11:07 am

    You can do this with Reflection. Assuming you have these variables:

    $class = 'Groups';
    $params = array(1, 'two');
    

    Then you can create a new instance of the class:

    $ref = new ReflectionClass( $class);
    $instance = $ref->newInstance();
    

    And call both methods the same way, checking if they’re static or not for completeness:

    $method = $ref->getMethod( 'fetchAll');
    $method->invokeArgs( ($method->isStatic() ? null : $instance), $params);
    
    $method = $ref->getMethod( 'alsoFetchAll');
    $method->invokeArgs( ($method->isStatic() ? null : $instance), $params);
    

    However, you don’t need to make sure they’re static, you can just as easily do this, regardless of whether or not the method is static:

    $ref->getMethod( 'fetchAll')->invokeArgs( $instance, $params);
    $ref->getMethod( 'alsoFetchAll')->invokeArgs( $instance, $params);
    

    You can see it working in this demo.

    Edit: Here is a demo showing that this works with the OP’s use case, without any errors / warnings / notices.

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

Sidebar

Related Questions

I need to be able to conditionally execute a method. I prefer not having
I have this script and need to be able to call the $play variable
I need to be able to call a method from a component located under
In C# it is not possible to override a static method. Nevertheless I have
When using extremely short-lived objects that I only need to call one method on,
Im just wondering whether there is a way to call a method where i
I have a static method: public class Example { //for demonstration purposes - just
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to access a list throughout my Rails project. For

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.