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 63k
  • Answers 63k
  • 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
  • added an answer How about something like: Using HTML Agility Pack HtmlDocument doc… May 11, 2026 at 10:25 am
  • added an answer Thanks for the answers so far. I just wanted to… May 11, 2026 at 10:25 am
  • added an answer (1) Your jQuery call $('imageCss') isn't a valid selector. You… May 11, 2026 at 10:25 am

Related Questions

In PHP, I am able to use a normal function as a variable without
In few months I start a project in PHP and I am hesitating to
Static variable gotcha in php I am from Java background and have switched to
I am starting a new web application in PHP and this time around I
I am a web-developer working in PHP. I have some limited experience with using
I am trying to code TDD style in PHP and one of my biggest
I am writing a custom session handler in PHP and trying to make the
I am writing helper classes for a large project in PHP and I have
I am doing the following in PHP: exec('java -jar /opt/flex3/lib/mxmlc.jar +flexlib /opt/flex3/frameworks MyAS3App.as -default-size
I am writing a web app in PHP, and it has come time to

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.