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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:31+00:00 2026-05-27T13:34:31+00:00

it is possible to assign to a class variable a function at runtime to

  • 0

it is possible to assign to a class variable a function at runtime to be executed? a kind of “function pointer” like C

something like this: (this won’t work because sum is out of the scope of A, but this is the pattern i mean)

 class A {
    public $function_name;
    public functon run($arg1,$arg2){
        $function_name($arg1,$arg2);
    }
}  

function sum($a,$b){
    echo $a+$b;
}

$a=new A();
$a->function_name='sum';

$a->run();     

[edit]
i know there is “call_user_func” but it need as i understand to have the function in the scope or use a public class method

  • 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-27T13:34:31+00:00Added an answer on May 27, 2026 at 1:34 pm

    You could use an anonymous function if you use PHP >5.3.0:

    $sum = function($a, $b) {
        return $a+$b;
    }
    
    $a->function_name = $sum;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Assign a value to class variable is assiging it for all instances
Is it possible to assign a value to a class variable from inside a
Is it possible to assign php array in MySQL IN() function? for example, $numbers
Using pointer arithmetic, it's possible to assign characters from one array to another. My
for instance in python it is possible to assign a method to a variable:
Possible Duplicate: Difference between class property mVar and instance variable self.mVar I am new
Is it possible to assign a value to an instance variable during an initialize
Is it possible to (or how can I) assign values to a class property
Under a table view delegate, indexPath is sent. Is it possible to assign this
Is it possible not to assign context to lambda? For example: class Rule def

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.