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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:04:46+00:00 2026-05-10T18:04:46+00:00

In PHP, I am able to use a normal function as a variable without

  • 0

In PHP, I am able to use a normal function as a variable without problem, but I haven’t figured out how to use a static method. Am I just missing the right syntax, or is this not possible?

(EDIT: the first suggested answer does not seem to work. I’ve extended my example to show the errors returned.)

function foo1($a,$b) { return $a/$b; }  class Bar {     static function foo2($a,$b) { return $a/$b; }      public function UseReferences()     {         // WORKS FINE:         $fn = foo1;         print $fn(1,1);          // WORKS FINE:         print self::foo2(2,1);         print Bar::foo2(3,1);          // DOES NOT WORK ... error: Undefined class constant 'foo2'         //$fn = self::foo2;         //print $fn(4,1);          // DOES NOT WORK ... error: Call to undefined function self::foo2()         //$fn = 'self::foo2';         //print $fn(5,1);          // DOES NOT WORK ... error: Call to undefined function Bar::foo2()                 //$fn = 'Bar::foo2';         //print $fn(5,1);       } }  $x = new Bar(); $x->UseReferences(); 

(I am using PHP v5.2.6 — does the answer change depending on version too?)

  • 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. 2026-05-10T18:04:47+00:00Added an answer on May 10, 2026 at 6:04 pm

    PHP handles callbacks as strings, not function pointers. The reason your first test works is because the PHP interpreter assumes foo1 as a string. If you have E_NOTICE level error enabled, you should see proof of that.

    ‘Use of undefined constant foo1 – assumed ‘foo1”

    You can’t call static methods this way, unfortunately. The scope (class) is relevant so you need to use call_user_func instead.

    <?php  function foo1($a,$b) { return $a/$b; }  class Bar {     public static function foo2($a,$b) { return $a/$b; }      public function UseReferences()     {         $fn = 'foo1';         echo $fn(6,3);          $fn = array( 'self', 'foo2' );         print call_user_func( $fn, 6, 2 );      } }  $b = new Bar; $b->UseReferences(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • 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 You can accomplish your task with just the second table/data… May 11, 2026 at 7:01 pm
  • Editorial Team
    Editorial Team added an answer Qt can transparently use resources. This means you can like… May 11, 2026 at 7:01 pm
  • Editorial Team
    Editorial Team added an answer You could expand your Product class to have a method… May 11, 2026 at 7:01 pm

Related Questions

I am using the simple modal and Ajax . When I try to read
I am currently working on a project where users need to be able to
I am working on developing a pair of libraries to work with a REST
I am working on an application that runs locally on a Fedora 10 machine

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.