static variables and caching

August 24, 2008 – 1:11 pm

If you’re working with classes a lot, you’ve probably come across the “static” keyword for properties and methods, enabling you to call these members straight on the class itself, without instantiating an object.


class UserPeer
{
protected static $userlist;

public static function getUserList()
{
return self::$userlist;
}
}
What’s a little less frequently encountered is a static variable inside a function (or a method for that matter). Say you have a function that returns something rather computing-intensive, but that’s called multiple times from different areas and you don’t want to re-compute every time, and you also don’t want to drag a global variable around.. then static is for you. Check this out:


function get_world_formula()
{
static $result;
if(!$result)
{
$result = //compute complex stuff here;
}
return $result;
}

Voila! A self-contained caching function. Obviously $result persists only for the duration of one request, but that’ll do just fine if it cuts down some CPU cycles. (It does add to the memory usage though, so be selective where you use it.) If you want to take this one step further, you can make caching optional by adding an extra parameter.


function get_world_formula($use_cache = true)
{
static $result;
if(!$result || !$use_cache)
{
$result = //compute complex stuff here;
}
return $result;
}

That’s it. Small but cute.

A word of OOP caution! I would not recommend using this approach in objects-methods, because this one static variable would be the same accross all object instances, so all objects would return the same data the first method call yielded, and this may not be appropriate for other objects. Use object properties instead to store chached results.


Filed under: Rnadom Sftuf — by Richtermeister

116 Comments »


  1. Medicamentspot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Online Pharmacy. Buy pills online

    Buy:Nexium.Arimidex.Mega Hoodia.Prednisolone.Prevacid.Valtrex.100% Pure Okinawan Coral Calcium.Accutane.Synthroid.Human Growth Hormone.Zyban.Actos.Zovirax.Lumigan.Petcam (Metacam) Oral Suspension.Retin-A….

    Trackback by JAMIE — July 21, 2010 @ 4:58 am


  2. CheapTabletsOnline.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. No prescription pills. Order drugs online

    Buy:Zocor.Aricept.Acomplia.Buspar.Zetia.Benicar.Cozaar.Amoxicillin.SleepWell.Advair.Female Pink Viagra.Ventolin.Prozac.Wellbutrin SR.Seroquel.Lasix.Nymphomax.Lipitor.Female Cialis.Lipothin….

    Trackback by ADAM — September 5, 2010 @ 9:40 am


  3. CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. No prescription drugs. Order pills online

    Buy:Retin-A.Zovirax.Arimidex.Prevacid.Synthroid.Valtrex.Human Growth Hormone.Lumigan.Petcam (Metacam) Oral Suspension.Actos.Nexium.Prednisolone.100% Pure Okinawan Coral Calcium.Accutane.Zyban.Mega Hoodia….

    Trackback by TERRENCE — September 6, 2010 @ 6:00 am


  4. NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

    Trackback by ANGEL — November 10, 2010 @ 6:24 pm

  5. cephalexin dosage for dogs

    Buygeneric drugs…

    Trackback by JEFFREY — December 22, 2010 @ 7:13 pm

  6. order cialis in canada online

    Buyno prescription…

    Trackback by ANDREW — December 23, 2010 @ 3:10 pm

  7. actonel@adverse.reactions” rel=”nofollow”>…

    Buygeneric meds…

    Trackback by DARRELL — December 27, 2010 @ 12:59 pm

  8. allopurinol@and.coumadin” rel=”nofollow”>…

    Buygeneric drugs…

    Trackback by SERGIO — December 30, 2010 @ 2:50 am

  9. avodart@in.usa” rel=”nofollow”>.

    Buyno prescription…

    Trackback by GUY — December 30, 2010 @ 2:16 pm

  10. ashwagandha@supplement.now” rel=”nofollow”>…

    Buygeneric meds…

    Trackback by JEROME — December 31, 2010 @ 2:33 am

  11. atarax@and.hives” rel=”nofollow”>.

    Buygeneric drugs…

    Trackback by SIDNEY — December 31, 2010 @ 9:25 am

  12. Chantix@official.site” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback by CECIL — January 2, 2011 @ 11:49 am

  13. cheap@Tramadol.now” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback by ALEJANDRO — January 4, 2011 @ 8:36 am

  14. generic@Cialis.now” rel=”nofollow”>.

    Buynow…

    Trackback by ENRIQUE — January 4, 2011 @ 12:20 pm

  15. Vitamin@B.now” rel=”nofollow”>.

    Buygeneric drugs…

    Trackback by DENNIS — January 4, 2011 @ 8:37 pm

  16. dicyclomine@and.imodium” rel=”nofollow”>…

    Buygeneric drugs…

    Trackback by SERGIO — January 8, 2011 @ 10:14 am

  17. Depakote@official.site” rel=”nofollow”>…

    Buyit now…

    Trackback by KIRK — January 21, 2011 @ 12:16 am

  18. depakote@classification.now” rel=”nofollow”>..

    Buygeneric drugs…

    Trackback by CURTIS — January 21, 2011 @ 12:59 pm

  19. side effects zyprexa overdose breathing autonomic

    Buyno prescription…

    Trackback by PHILIP — February 3, 2011 @ 4:28 pm

  20. e-code for zometa

    Buyit now…

    Trackback by DARRYL — February 4, 2011 @ 12:13 pm

  21. what works better for afib than amiodarone

    Buygeneric drugs…

    Trackback by ANGEL — February 6, 2011 @ 3:43 am

  22. cats zanaflex

    Buydrugs without prescription…

    Trackback by MAX — February 6, 2011 @ 11:58 am

  23. xopenex coupon

    Buyno prescription…

    Trackback by DANIEL — February 6, 2011 @ 8:25 pm

  24. zyprexa increase eps complaints

    Buyno prescription…

    Trackback by RONNIE — February 6, 2011 @ 10:38 pm

  25. Zyrtec

    Buyit now…

    Trackback by TROY — March 3, 2011 @ 4:23 pm

  26. Rogaine

    Buyno prescription…

    Trackback by FERNANDO — March 5, 2011 @ 8:35 pm

  27. Seroquel

    Buywithout prescription…

    Trackback by ANDRE — March 8, 2011 @ 1:55 am

  28. Synthroid

    Buywithout prescription…

    Trackback by VIRGIL — March 8, 2011 @ 11:44 pm

  29. Zyprexa

    Buynow it…

    Trackback by JEFF — March 9, 2011 @ 5:59 am

  30. Zyprexa

    Buyno prescription…

    Trackback by TYRONE — March 9, 2011 @ 5:33 pm

  31. Purim

    Buyno prescription…

    Trackback by MARION — March 9, 2011 @ 10:37 pm

  32. Prozac

    Buygeneric meds…

    Trackback by LUIS — March 10, 2011 @ 4:51 pm

  33. Reglan…

    Buywithout prescription…

    Trackback by ALFREDO — March 11, 2011 @ 7:42 am

  34. Pyridium@Pyridium.Pyridium” rel=”nofollow”>…

    Buygeneric meds…

    Trackback by ADRIAN — March 28, 2011 @ 6:38 am

  35. Tylenol@Tylenol.Tylenol” rel=”nofollow”>…

    Buygeneric meds…

    Trackback by BRIAN — March 28, 2011 @ 4:24 pm

  36. Topamax@Topamax.Topamax” rel=”nofollow”>.

    Buyit now…

    Trackback by JARED — March 28, 2011 @ 6:07 pm

  37. Prozac@Prozac.Prozac” rel=”nofollow”>…

    Buynow…

    Trackback by JIMMIE — March 29, 2011 @ 1:19 am

  38. Pulmicort@Pulmicort.Pulmicort” rel=”nofollow”>..

    Buyit now…

    Trackback by EARL — March 29, 2011 @ 1:13 pm

  39. Sleep@Tea.now” rel=”nofollow”>..

    Buyno prescription…

    Trackback by ERIK — March 29, 2011 @ 4:51 pm

  40. Prozac@Prozac.Prozac” rel=”nofollow”>…

    Buygeneric drugs…

    Trackback by JIMMY — March 29, 2011 @ 7:41 pm

  41. Strattera@Strattera.Strattera” rel=”nofollow”>…

    Buynow it…

    Trackback by WILLIE — March 30, 2011 @ 9:03 am

  42. Prozac@Prozac.Prozac” rel=”nofollow”>.

    Buywithout prescription…

    Trackback by BENJAMIN — March 30, 2011 @ 8:21 pm

  43. Cozaar@Cozaar.Cozaar” rel=”nofollow”>.

    Buynow it…

    Trackback by JULIO — March 30, 2011 @ 9:27 pm

  44. insulin@glargine.la” rel=”nofollow”>.

    Buygeneric pills…

    Trackback by GREG — April 2, 2011 @ 1:29 am

  45. .

    Buynow…

    Trackback by LLOYD — April 3, 2011 @ 12:38 am

  46. rhinocort@vs.beconase” rel=”nofollow”>.

    Buyno prescription…

    Trackback by RALPH — April 3, 2011 @ 10:05 am

  47. can@cymbalta.cause.vaginal.itching.and.irritation” rel=”nofollow”>..

    Buydrugs without prescription…

    Trackback by JARED — April 3, 2011 @ 8:03 pm

  48. advair@side.effects” rel=”nofollow”>.

    Buyit now…

    Trackback by ANDREW — April 3, 2011 @ 11:20 pm

  49. .

    Buyno prescription…

    Trackback by JERRY — April 4, 2011 @ 3:15 am

  50. .

    Buywithout prescription…

    Trackback by WARREN — April 4, 2011 @ 8:42 am

  51. long@term.prilosec.use” rel=”nofollow”>.

    Buygeneric drugs…

    Trackback by TROY — April 4, 2011 @ 12:26 pm

  52. abilify@and.olanzapine” rel=”nofollow”>.

    Buyit now…

    Trackback by THOMAS — April 5, 2011 @ 5:19 pm

  53. side@effects.of.dog.medication.famotidine.and.flagyl” rel=”nofollow”>…

    Buygeneric drugs…

    Trackback by OLIVER — April 5, 2011 @ 7:24 pm

  54. pediatric@dosage.of.prednisolone” rel=”nofollow”>…

    Buyno prescription…

    Trackback by ANTONIO — April 5, 2011 @ 9:27 pm

  55. wholesale@ginseng.root” rel=”nofollow”>…

    Buyno prescription…

    Trackback by JIMMY — April 6, 2011 @ 10:10 pm

  56. Buygeneric drugs…

    Trackback by ERNESTO — April 7, 2011 @ 12:57 am

  57. Purchase@Cheap.Coral.Calcium” rel=”nofollow”>..

    Buynow it…

    Trackback by LEWIS — July 29, 2011 @ 12:51 am

  58. Get@Abana.Online” rel=”nofollow”>..

    Buyit now…

    Trackback by TERRENCE — July 29, 2011 @ 7:58 am

  59. Get@Abilify.Online” rel=”nofollow”>..

    Buygeneric drugs…

    Trackback by DUSTIN — July 29, 2011 @ 1:26 pm

  60. Order@Generic.Abilify” rel=”nofollow”>…

    Buywithout prescription…

    Trackback by PERRY — July 29, 2011 @ 3:02 pm

  61. Cheap@Abilify.Without.Prescription” rel=”nofollow”>.

    Buynow it…

    Trackback by ENRIQUE — July 29, 2011 @ 5:06 pm

  62. Buy@Coral.Calcium.Online” rel=”nofollow”>.…

    Buygeneric meds…

    Trackback by ARTHUR — July 31, 2011 @ 9:54 pm

  63. Order@Coral.Calcium.Online” rel=”nofollow”>..

    Buygeneric pills…

    Trackback by FREDDIE — July 31, 2011 @ 11:03 pm

  64. Order@Cheap.Abilify” rel=”nofollow”>..

    Buyno prescription…

    Trackback by GABRIEL — August 1, 2011 @ 9:14 am

  65. Buy@Abilify.Online” rel=”nofollow”>..

    Buyno prescription…

    Trackback by BRANDON — August 2, 2011 @ 12:28 pm

  66. Order@Acai.Online” rel=”nofollow”>…

    Buygeneric drugs…

    Trackback by WALLACE — August 3, 2011 @ 4:23 pm

  67. Order@Discount.Acai” rel=”nofollow”>…

    Buynow…

    Trackback by ALFRED — August 3, 2011 @ 6:51 pm

  68. Purchase@Acai.Online” rel=”nofollow”>…

    Buynow…

    Trackback by KIRK — August 3, 2011 @ 8:22 pm

  69. Cheap@Generic.Accutane” rel=”nofollow”>.…

    Buygeneric drugs…

    Trackback by BILLY — August 6, 2011 @ 4:53 pm

  70. Buy@Acomplia.Online” rel=”nofollow”>.…

    Buynow it…

    Trackback by KENNY — August 9, 2011 @ 12:09 am

  71. Buy@Discount.Acomplia” rel=”nofollow”>.…

    Buygeneric meds…

    Trackback by CARL — August 9, 2011 @ 2:20 am

  72. Order@Cheap.Acomplia” rel=”nofollow”>.…

    Buyno prescription…

    Trackback by ADAM — August 9, 2011 @ 4:38 am

  73. Cheap@Generic.Actonel” rel=”nofollow”>…

    Buywithout prescription xiu…

    Trackback by LLOYD — August 10, 2011 @ 2:13 pm

  74. Cheap@Actonel.35mg” rel=”nofollow”>…

    Buyit now oil…

    Trackback by LUIS — August 10, 2011 @ 3:16 pm

  75. Order@Generic.Actos” rel=”nofollow”>..

    Buygeneric meds qmo…

    Trackback by CLIFTON — August 11, 2011 @ 12:11 pm

  76. Order@Actos.Without.Prescription” rel=”nofollow”>..

    Buygeneric pills zdg…

    Trackback by VICTOR — August 11, 2011 @ 12:43 pm

  77. Purchase@Adalat.Online” rel=”nofollow”>.

    Buyno prescription luw…

    Trackback by SERGIO — August 12, 2011 @ 12:10 pm

  78. Purchase@Generic.Abilify” rel=”nofollow”>..

    Buygeneric drugs krg…

    Trackback by DARYL — August 13, 2011 @ 1:11 pm

  79. Cheap@Acai.Without.Prescription” rel=”nofollow”>…

    Buydrugs without prescription jlg…

    Trackback by BRETT — August 14, 2011 @ 7:12 pm

  80. Buy@Discount.Energy.Boost” rel=”nofollow”>.

    Buygeneric drugs itj…

    Trackback by STEVEN — August 15, 2011 @ 7:37 am

  81. Buy@Discount.Accutane” rel=”nofollow”>.

    Buygeneric drugs nih…

    Trackback by BRYAN — August 16, 2011 @ 6:26 pm

  82. Order@Cheap.Accutane” rel=”nofollow”>..

    Buyno prescription tao…

    Trackback by GLENN — August 16, 2011 @ 8:34 pm

  83. Buy@Accutane.10mg.20mg” rel=”nofollow”>…

    Buynow it zdg…

    Trackback by BRUCE — August 17, 2011 @ 3:39 pm

  84. Buy@Cheap.Actonel” rel=”nofollow”>…

    Buydrugs without prescription vrg…

    Trackback by DENNIS — August 21, 2011 @ 6:03 am

  85. Cheap@Actonel.Online” rel=”nofollow”>..

    Buydrugs without prescription faj…

    Trackback by ROLAND — August 21, 2011 @ 7:27 pm

  86. Get@Actonel.Online” rel=”nofollow”>..

    Buygeneric drugs ajd…

    Trackback by ALEX — August 21, 2011 @ 8:48 pm

  87. Purchase@Advair.Online” rel=”nofollow”>..

    Buyno prescription jui…

    Trackback by BRUCE — August 23, 2011 @ 3:43 am

  88. Buy@Generic.Advair” rel=”nofollow”>.

    Buygeneric drugs ouw…

    Trackback by DWAYNE — August 23, 2011 @ 11:42 am

  89. Buy@Advair.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs hwi…

    Trackback by SHANE — August 23, 2011 @ 12:59 pm

  90. Order@Advair.Without.Prescription” rel=”nofollow”>.

    Buyit now ojb…

    Trackback by VINCENT — August 23, 2011 @ 4:58 pm

  91. Cheap@Generic.Advair” rel=”nofollow”>.

    Buydrugs without prescription xab…

    Trackback by VINCENT — August 23, 2011 @ 9:00 pm

  92. Purchase@Cheap.Albenza” rel=”nofollow”>…

    Buyit now knc…

    Trackback by NATHAN — August 25, 2011 @ 1:46 pm

  93. accutane@verdict.buy” rel=”nofollow”>..

    Buydrugs without prescription…

    Trackback by STEPHEN — August 28, 2011 @ 7:49 am

  94. adalat@asus.buy” rel=”nofollow”>.

    Buydrugs without prescription…

    Trackback by DOUGLAS — August 28, 2011 @ 1:56 pm

  95. allopurinol@atrial.fibrillation” rel=”nofollow”>.

    Buygeneric pills…

    Trackback by STEPHEN — August 28, 2011 @ 5:07 pm

  96. norvasc and theophylline

    Buy_drugs without prescription…

    Trackback by JERRY — October 17, 2011 @ 3:01 pm

  97. c-section scar pain symptoms pregnancy

    Buy_generic drugs…

    Trackback by JAMES — October 17, 2011 @ 6:22 pm

  98. over the counter birth control

    Buy_generic meds…

    Trackback by EDWIN — October 18, 2011 @ 3:38 pm

  99. birth control pills for pms symptoms

    Buy_drugs without prescription…

    Trackback by RAY — October 20, 2011 @ 2:21 am

  100. otc proton pump inhibitors

    Buy_no prescription…

    Trackback by TRAVIS — October 20, 2011 @ 2:00 pm

  101. gall bladder cancer surgery

    Buy_it now…

    Trackback by MARVIN — October 20, 2011 @ 8:38 pm

  102. rapid weight loss

    Buy_generic drugs…

    Trackback by CHRIS — October 21, 2011 @ 6:49 pm

  103. cheap travel nebulizer

    Buy_generic drugs…

    Trackback by BRYAN — October 22, 2011 @ 1:36 am

  104. how strength training effects osteoporosis

    Buy_generic drugs…

    Trackback by MARK — October 22, 2011 @ 1:54 pm

  105. xanax and pregnancy

    Buy_generic pills…

    Trackback by TRAVIS — October 22, 2011 @ 5:15 pm

  106. lidocaine injectable

    Buy_it now…

    Trackback by ALEX — October 27, 2011 @ 8:15 am

  107. dhea treatment of depression

    Buy_drugs without prescription…

    Trackback by HOMER — October 28, 2011 @ 2:17 pm

  108. pregnancy issues with men

    Buy_no prescription…

    Trackback by JEFF — October 29, 2011 @ 2:25 pm

  109. sandwich elisa for hepatitis b

    Buy_generic meds…

    Trackback by WARREN — November 3, 2011 @ 1:53 am

  110. colon cancer lymph node

    Buy_without prescription…

    Trackback by FREDRICK — November 4, 2011 @ 12:01 pm

  111. cipro for sinus infection

    Buy_generic meds…

    Trackback by CHRIS — November 4, 2011 @ 3:22 pm

  112. symtons of canine gall bladder disease

    Buy_generic meds…

    Trackback by ALEX — November 5, 2011 @ 5:17 pm

  113. seed implant for prostate cancer

    Buy_drugs without prescription…

    Trackback by RAMON — November 8, 2011 @ 5:47 am

  114. adult male wheezing new onset

    Buy_drugs without prescription…

    Trackback by FRANCIS — November 10, 2011 @ 1:32 am

  115. prozac overdose and tardive dyskinesia

    Buy_generic meds…

    Trackback by OSCAR — November 15, 2011 @ 11:30 pm

  116. children during the depression

    Buy_drugs without prescription…

    Trackback by JIM — November 17, 2011 @ 2:10 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress