Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. <?php
  2. namespace App\Traits;
  3.  
  4. trait HasMemoization
  5. {
  6. protected static $memoized = [];
  7.  
  8. /**
  9. * Memoize Operation Result
  10. * @param $key
  11. * @param \Closure $callback
  12. * @param bool $refresh
  13. * @return mixed
  14. */
  15. public function memoize($key, \Closure $callback, $refresh = false){
  16. if(!isset(static::$memoized[$key]) || $refresh){
  17. return static::$memoized[$key] = $callback();
  18. }
  19. return static::$memoized[$key];
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement