Skip to content

Instantly share code, notes, and snippets.

@TitasGailius
Created January 16, 2017 22:08
Show Gist options
  • Save TitasGailius/93ab58451505917b577ec12326d00aa4 to your computer and use it in GitHub Desktop.
Save TitasGailius/93ab58451505917b577ec12326d00aa4 to your computer and use it in GitHub Desktop.
<?php
/**
* Let's pretend that we have this expensive
* function that takes up to 4 seconds to run
* and in this case, total time of execution
* would be 8 seconds.
*
*/
function buildRocket() : boolean
{
sleep(4);
return true;
}
buildRocket();
buildRocket();
/**
* Let's wrap it up with once() helper function.
* Total time of execution now would be only 4 seconds
* because it cached result of the function and yes
* it would handle different arguments passed as well.
*/
function buildRocket() : boolean
{
return once(function () {
sleep(4);
return true;
});
}
buildRocket();
buildRocket();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment