1

I'm trying to declare a function and store it on a private static field of my class. I've got something like this:

class MyClass {
    private static $myFunction = function() { /* stuff here */ };
}

But I keep getting this error on the line that creates the function:

PHP Parse error: syntax error, unexpected 'function' (T_FUNCTION)…

I'm doing this based on this answer, as well as on what the PHP manual pages say. But it is not working for me. Why? what can I do?

The goal for all of this is that I could be able store the function on an array:

private static $options = [
    'function' => MyClass::$myFunction
];

So what do you think is the best way to achieve this? I'm using PHP 5.5.14 in case you wonder.

Update:

I've tried a couple of different approaches. Like this:

class MyClass {
    private static function myFunction() { /* Expression */ }
    private static $options = [
        'function' => MyClass::myFunction()
    ];
}

But it throws me errors about an unexpected '(':

PHP Parse error: syntax error, unexpected '(', expecting ']'…

And this. Which is the only one that works:

class MyClass {
    private static function myFunction() { /* Expression */ }

    public static function anotherFunction() {
        $options = [ 'function' => MyClass::myFunction() ];
    }
}

But I need to have this $options var to be accessible to more methods on the same class, so it's not a solution.

I'd prefer to stay away from constructors, since it is only a helper class and I don't want to mess with instances of it and such things.

Community
  • 1
  • 1
arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • 1
    the manual says: 'Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.'. [more details here](http://php.net/manual/en/language.oop5.static.php) – Ryan Vincent Jul 02 '14 at 04:19
  • Thank you, now I'm aware of that. Please check my updated answer for the approaches I've tried so far. – arielnmz Jul 02 '14 at 04:36
  • If it's only a helper class then why does it need to be a class at all? – Ignacio Vazquez-Abrams Jul 02 '14 at 04:37
  • It's a helper class with methods for hashing and verifying passwords, I think it'd be a good idea to encapsulate them within a class, for design reasons. – arielnmz Jul 02 '14 at 04:38
  • Like you, i haven't found a really 'neat' way of getting the static variables initialized. i have used the 'initFunction' as you mention but getting that function to run can be 'clumsy'. The easiest way is just call the 'initFunction' in the class definition script but can be confusing for maintenance as you don't expect code to be called when defining classes. – Ryan Vincent Jul 02 '14 at 04:39
  • @RyanVincent I agree, it is the easiest way indeed, and I think I'm stuck with it. A workaround for it is to make the *initFunction* to be called from all methods whenever the value of `$options` hasn't been initialized yet (since I need that `$options` on several different methods), but I think it's too much mess for a (relatively) simple task. – arielnmz Jul 02 '14 at 04:45

1 Answers1

1

You could use this approach

class MyClass{
    private static function myFunction(){ echo __FUNCTION__;}
    private static $options = [
        'function' => ['MyClass', 'myFunction']
    ];

    public static function test(){
        $function = self::$options['function'];
        $function();
    }

    }
MyClass::test();

It's possible because array could be callable.

sectus
  • 15,605
  • 5
  • 55
  • 97
  • Can you elaborate a bit more (or post a reference) on why should I need to have an array in the form of `['ClassName', 'method']` in order for this to work? Why couldn't `['function => 'myFunction']` be enough, as the PHP manual for [variable functions](http://www.php.net/manual/en/functions.variable-functions.php) suggests? This is most certainly what I need. – arielnmz Jul 02 '14 at 05:53
  • It's reference to variable function. But you need to call static method. There some examples about: http://php.net/manual/en/language.types.callable.php – sectus Jul 02 '14 at 06:01
  • Now that makes sense. Well, I've revised the design again and it turns out that I really don't need to hold a reference to the function in this case, but I'd certainly need to sometime in the future, and this will come in handy. Thanks for your answer. – arielnmz Jul 02 '14 at 06:03
  • @sectus : just out of curiosity, whats `echo __FUNCTION__;` supposed to do!? – NoobEditor Jul 02 '14 at 06:19
  • 1
    @NoobEditor, it's one of [magic constants](http://www.php.net/manual/en/language.constants.predefined.php). – sectus Jul 02 '14 at 06:20