Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to static method in PHP?

Tags:

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?)

like image 808
Eric Avatar asked Oct 10 '08 16:10

Eric


People also ask

What are static methods in PHP?

Definition and Usage. The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

How do you refer to a static variable in PHP?

Accessing the value of a static variable is similar to accessing for a class constant: You either use the type name (outside of or within the class) or the keyword self , both followed by the scope resolution operator ( :: ) and the name of the static variable, starting with $ .

Can static methods use the reference this?

Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method.

Can we override static method in PHP?

Here comes the call of static method with static keyword. In case there is no overridden function then it will call the function within the class as self keyword does. If the function is overridden then the static keyword will call the overridden function in the derived class.


1 Answers

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(); 
like image 106
Peter Bailey Avatar answered Sep 30 '22 05:09

Peter Bailey