0

I wanted to have a static closure variable in my class, so that people can change the behavior of a specific part of the code. However, I can't seem to be able to initialize it anywhere.

First I tried this:

public static $logger = function($sql) { print_r($sql); };

But apparently PHP can't handle that. Ok, so I made a static init method:

public static $logger;

static function init() {
    /* if (!Base::logger) */
    Base::logger = function($sql) { print_r($sql); };
}

And call it at the end of the file, outside class definition. But this also give me a syntax error: Parse error: syntax error, unexpected '=' in [file] on line [line]. Any hints?

1
  • 2
    YOu simply forgot a $ : Base::$logger = Commented Feb 8, 2014 at 11:09

1 Answer 1

1

The syntax error is right where the error message tells you it is (it would have been even easier to spot if you had given us line numbers...): a missing $-sign.

Base::$logger = function (...)

In addition to that, you migth want to use self:: instead of Base::, this ensure the code will work without any additional changes if you ever rename the class

self::$logger = function (...)

You can improve this code even further, when changing the initializer to a getter that JIT-creates the closure:

private static $logger = NULL;

public static function getLogger () {
    if (self::$logger === NULL) {
        self::$logger = function ($sql) {print_r($sql);};
    }
    return self::$logger;
}

[Edit] Based on your comment on this: the clean OOP way of being able to change $logger would be to use a setter:

public static function setLogger ($closure) {
    self::$logger = $closure;
}

COmbining this and the getter from above ensures that you always get the value set by the setter, and, if none has been set yet, the default value. Using the setter to set the value back to NULL makes the getter create the default again, which is anoth er plus.

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

5 Comments

Oh, thanks a lot haha It's confusing because when accessing non static fields I don't need the $ there... I wonder how I'd do if I wanted to read the name from a variable, like I can do with non-static fields: $foo = 'logger'; self::$foo ...
And: use double-$ to have variable variable [sic!] names: $foo = 'logger'; self::$$foo is the way to go.
That I can't do, because the point of having the closure is allowing the user of the class change the $logger to do whatever he wants... So it must be set to default only once, at start, and if it is null it means he set it to null and doesn't want to log anything. But that's a nice way of doing it for something else :)
See your point. Yes, not possible in your case, I agree (unless you're using a setter-method, which would be the clean OOP way of doing it ;) )
Edited again to reflect that.

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.