Reputation: 1501
I know this code works but I don't know if it'd be valid to do. The reason I wanted to support this is to allow people to overwrite this function with another file. Sounds slightly strange, but for my purpose it makes sense.
I only ask because I seriously can't find an example of this being done and have been searching for an hour or so.
$jerry = function(){
return 'hello world';
};
echo $jerry();
Upvotes: 0
Views: 20
Reputation: 13283
Yes, it is valid to use anonymous functions assigned to variables.
You should make use of the PHP documentation (under "Language Reference" > "Functions" > "Anonymous functions") when you are not sure about something. The website is actually quite easy to use.
Another way of finding stuff on php.net is to use the URL path. For example, if you are interested in functions then you can just go to php.net/functions, and it will attempt to direct you to the right place. This will work with quite a lot of stuff; php.net/juggling, for instance, will direct you to the "type juggling" page. If it is unable to direct you to something relevant then it will show you search results instead, using the URL path as the search query.
Upvotes: 2
Reputation: 939
I believe you can do something else... say you have
function1() {
//code for function1 here
}
function2() {
//code for function2 here
}
you can actually do this
$var = 'function1';
$var(); //runs function1()
$var = 'function2';
$var(); //runs function2()
Hope it clears out things
Otherwise all the information you seek is in www.php.net/functions as the fellow user pointed out earlier. Learn to search php.net for any issues, at the bottom of every documentation page there are EXAMPLES that 95% help you for what you're looking to achieve
Upvotes: 0