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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:13:36+00:00 2026-06-11T23:13:36+00:00

I would like to assign a static function to a variable so that I

  • 0

I would like to assign a static function to a variable so that I can send it around as a parameter. For example:

class Foo{
    private static function privateStaticFunction($arg1,$arg2){
      //compute stuff on the args          
    }

    public static function publicStaticFunction($foo,$bar){

         //works
         $var = function(){
                   //do stuff
                };

         //also works
         $var = function($someArg,$someArg2){
                   //do stuff
         };

         //Fatal error: Undefined class constant 'privateStaticFunction'                    
         $var = self::privateStaticMethod;

         //same error
         $var = Foo::privateStaticFunction;

         //compiles, but errors when I try to run $var() somewhere else, as expected
         //Fatal error: Call to private method Foo::privateStaticMethod() from context ''
         $var = function(){
             return Foo::privateStaticMethod(); 
         }; 
    }
}

I’ve tried a few more variations but none of them worked.

I don’t even expect this sort of functional hacking to work with PHP but hey, who knows?

Is it possible to do that in PHP or will I need to come up with some hack using eval?

P.S.: LawnGnome on ##php mentioned something about it being possible to do what I want using array('Foo','privateStaticMethod') but I didn’t understand what he meant and I didn’t press him further as he looked busy.

  • 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-11T23:13:37+00:00Added an answer on June 11, 2026 at 11:13 pm

    You want to use call_user_func with an array argument.

    $var = array('Foo','privateStaticMethod');
    call_user_func($var);
    

    Note that the “private” specifier on that method will make it only callable this way within the Foo class.

    call_user_func takes a callable as the first argument. Note that as of PHP 5.2.3, it is possible to avoid the array notation in your case.

    Static class methods can also be passed without instantiating an
    object of that class by passing the class name instead of an object at
    index 0. As of PHP 5.2.3, it is also possible to pass
    ‘ClassName::methodName’.

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

Sidebar

Related Questions

I have the following variable in a class named Example: private static int number;
I would like to assign a static list of items in a SelectList() to
I would like to assign a variable instead of the string in my following
I have a JQuery plugin function where I would like to assign selectors from
I would like to try to assign a variable value to input hidden field
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
I've got a class that extends JFrame... I then assign it to a variable
I have a few static unordered_maps in my application that I would like to
I've got a static property I would like to access by importing it's class
I would like to assign an event listener to a protected timer variable in

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.