PHP opening/closing tag
[Link],Example:<?=$var?>. AlwaysOMITtheclosingtag.
Single and double quotes
Usedoublequoteswhenyouhavevariableinthestring,[Link] ignoreeverythingwithinsinglequotes,thereforefasterprocessing. Example echo <a href=$url>Link</a>; echo <a href=hello/world>Link</a>;
Brace style
if (condition) { action1(); } elseif (condition) { action2(); } else { action3(); }
Naming convention
[Link] separatedwithunderscore. Incorrect class userCollection class UserCollection Correct class User_collection
[Link] separatedwithcamelCase. Incorrect function getcew() function GetCrew() function get_crew() Correct function getCrew() Privatemethodsshouldbeprefixedwithandunderscore Example private function _getUserCrew() DBcollectionname&[Link] shouldbeseparatedbyunderscore. Incorrect $usercrew $userCrew $User_crew Correct $user_crew
Comparing boolean values
Alwaysuse===and!==[Link] or0wouldbeevaluatetoFALSEinloosecomparisons.
Typecasting
AlwaystypecastnumericvaluesbeforeUpdating/InsertingintotheMongoDB. ThisistoavoidsavingInteger/FloatvalueasStringinDB,whichwillhaveproblem whenyoudoquery.
DRY - Dont Repeat Yourself
[Link],createa methodoralibrary. IfsamepieceofcodeisrequiredonseverallocationwithinthesameClass,please createanewmethodforit. IfsamepieceofcodeisrequiredonseveralClass,pleasecreateanewlibraryforit.
Clever code
Ingeneral,readabilityismoreimportantthanclevernessorbrevity.
Redirect
Minimizeredirect()usage,whenrequiredtouse,alwaysdie()orexit()immediatelyafter redirect().
Memcached Usage
Use<prefix>_<key>_<id>formatformemcachedkeywheneverpossible. Prefixpsshortforpirateship. [Link]:user_crew,fruit_skill,fruit_exp,quest_area... IDtheassociatedIDfromDB. InfullyourMCkeymaylooklikethat: ps_user_crew_[user_id]>storingtheuserscrewdbdata. [Link] aretheavailableMCavailabletouse/clear/update&toreducechancesofsavingthesamepiece ofdataonseveralMC. Whenqueryingalistofdata,likeallgameitem,allcrew,allfruits,pleaseusethefollowing approach,usinguserscrewasexample: 1. QueryforlistofIDonly 2. StorelistofIDsinMC,ps_user_crew_list_[user_id] 3. StoreindividualusercrewdataindifferentMC,ps_user_crew_[user_crew_id] 4. LoopthroughcrewlistandgetcrewdatafromspecificMC 5. Querywhenneededandremembertoclear/updateMCwhenthereschanges Whenusingalibrary,thelibraryshouldmanagerelatedMC,andprovidewaystocleartheMC. Example: class Crew_library { var $enable_mc = TRUE; var $ci; function __construct() { $ci = &get_instance();
} function getUserCrew($id) { $data = array(); if ($this->enable_mc === TRUE) // note that MC key is stored in config file $data = $this->memcached_library->get(MC_USER_CREW . $id); if (empty($data)) { $data = $this->ci->user_crew_model->getById($id); if (!empty($data)) $this->ci->memcached_library->set(MC_USER_CREW . $id, $data, 3600); } return $data; } function clearUserCrewMc($id) { $this->ci->memcached_library->delete(MC_USER_CREW . $id); return TRUE; } } Note:
Haveaon/offswitchforeachclassforMC DonotdependentirelyonMC,alldevelopmentshouldbedonewithMCswitchedoff Remembertosetalogicalexpirytime Gameshouldwork100%withoutMC Remembertoclear/updateMClistwhennewdataisinsertedviaAdmin