0

I'am trying to make a reference to a static function inside a class:

class Test {

    function __construct() {

        $this->fn1 = self::fn2;


    }

    public static function fn2() {

    }


}

then i get this error:

Undefined class constant 'fn2'

why?

3
  • 1
    Do you have code showing what you tried to do which caused the error? Also, your paste is missing a close brace on fn2(). And that self: needs two colons. And heck, you forgot to put parens on fn2... geez, did you vett any of this code? Commented Oct 16, 2017 at 1:57
  • @Randall I'am trying to pass a reference, thats why its without closing brackets, that is the error that it wont passing the reference Commented Oct 16, 2017 at 2:09
  • Possible duplicate of Reference to static method in PHP? Commented Oct 16, 2017 at 2:29

2 Answers 2

0

Not sure if this is what you want, but at least this might give you a hint:

<?php

class Test {

    function __construct() {

        $this->fn = function(){
             return self::realFn();
        };

    }

    public function callFn (){
        $fn = $this->fn ;//yes, assigning to a var first is needed. You could also use call_user_func
        $fn();
    }
    public static function realFn() {
        echo 'blah';
    }


}

$x = new Test();

$x->callFn();

You can test it here: https://3v4l.org/KVohi

Sign up to request clarification or add additional context in comments.

Comments

0

Updated

If you want to assign a function to a variable, it is best to do this with annonymous aka lambda functions since they are first class citizens and may be freely passed, returned and assigned. PHP is not unique in dealing with static method references in this fashion as JAVA implements them similarly:

Method references ... are compact, easy-to-read lambda expressions for methods that already have a name.

You may create an anonymous function based on a callable in PHP, and so the OP may wish to do as follows, which PHP 7.1.10 or higher supports:

<?php
class Test {
    public static function fn2() {
         return __METHOD__;
    }
    public static function getClosure (){
        return Closure::fromCallable(["Test","fn2"]);
    }
}


echo Test::getClosure()(),"\n";

See live code here

In this example an anonymous function is created and returned by the static getClosure method. When one invokes this method, then it returns the closure whose content is the same as static method fn2. Next, the returned closure gets invoked which causes the name of static method fn2 to display.

For more info re closures from callables, see the Manual and the RFC.

With PHP 7 on up, you may create a complex callable. In the code below the complex callable is an invocable array:

<?php
class foo 
{
    public static function test()
    {   
        return [__CLASS__, 'fn2'];
    }   

    public static function fn2()
    {   
        echo __METHOD__;
    }   
}

echo foo::test()();

See live code.

Note: Starting with PHP 7.0.23 you could create a complex callable using a string containing the class and method names separated by the double colon aka paaamayim nekudotayim; see here.

A solution that has broader PHP support is as follows:

<?php

class Test {

    public static function fn2() {
           return __METHOD__;
    }
    public static function tryme(){
      return call_user_func(["Test","fn2"]);
    }

}
// return closure and execute it
echo Test::tryme();

See live code

4 Comments

but I need a reference to that function, not his result
Then you may wish to convert that method into an annonymous function and assign it to a variable that gets returned.
where should i put that anonymous function?
@Skrudox Please see my updated answer which I hope will prove helpful to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.