Posted in PHP

Lambda expression

This is saved from an answer by Staplerfahrer on stackoverflow. As this library of snippets is mostly for my own personal reference I’m stealing it and including it here for later use!

I don’t like function_exists('fun_name')┬ábecause it relies on the function name being turned into a string, plus, you have to name it twice. Could easily break with refactoring.

Declare your function as a lambda expression:

$generate_salt = function()

And use thusly:

$salt = $generate_salt();

Then, at re-execution of said PHP code, the function simply overwrites the previous declaration.

Leave a Reply

Your email address will not be published. Required fields are marked *