Created
January 16, 2017 22:08
-
-
Save TitasGailius/93ab58451505917b577ec12326d00aa4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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