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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:50:50+00:00 2026-06-03T17:50:50+00:00

I am developing on Symfony2 and I need to call a method on a

  • 0

I am developing on Symfony2 and I need to call a method on a class, both known only at runtime.

I have already successfully used variable functions and call_user_func in the project, but this time they give me problems…

My code looks like this

namespace MyBundleNamespace;

use MyBundle\Some\Class;

class MyClass
{
    public static function myFunction() { ... }
}

and in some other file I need to do this

MyClass::myFunction();

but dynamically, so I tried both

$class = "MyClass";
$method = "myFunction";

$class::$method();

and

$class = "MyClass";
$method = "myFunction";
call_user_func("$class::$method");

But I get a class MyClass not found error. Of course the class is included correctly with use and if I call MyClass::myFunction() just like that it works.

I also tried to trigger the autoloader manually like suggested in this question answer comment, but it did not work. Also, class_exists returned false.

What am I missing? Any ideas?

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-06-03T17:50:54+00:00Added an answer on June 3, 2026 at 5:50 pm

    You’re missing the namespace:

    $class = '\\MyBundleNamespace\\MyClass';
    $method = 'myFunction';
    

    Both calls should work:

    call_user_func("$class::$method");
    call_user_func(array($class, $method));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a simple page with Symfony2, using Twig as template engine. I have
developing iphone app, I have used a UIImageview and i have set an image
Developing an MVC application, i now need to have test other browser versions. Installed
I have a question about Symfony2 performance. I have been developing with Symfony2 under
I am developing an web application in symfony2. I have implemented login and logout
Developing a Django web app, I have a list of packages I need to
I am developing an application using Symfony2. I am using Twig for templating an
I am developing an application using Symfony2 and DQL for building some queries in
I am developing an application using Symfony2 and Jquery as a JavaScript FW. I
I am developing an application using symfony2. I would like to know how I

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.