Sei sulla pagina 1di 4

PHP opening/closing tag

AlwaysusefullPHPopeningtag.DonotuseshorthandPHPtags,Example:<?=$var?>. AlwaysOMITtheclosingtag.

Single and double quotes


Usedoublequoteswhenyouhavevariableinthestring,usesinglequoteforanythingelse.PHP 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
Classnameshouldalwaysstartwithanuppercase.Multiplewordsshouldbe separatedwithunderscore. Incorrect class userCollection class UserCollection Correct class User_collection

Methodnameshouldalwaysstartwithanlowercase.Multiplewordsshouldbe separatedwithcamelCase. Incorrect function getcew() function GetCrew() function get_crew() Correct function getCrew() Privatemethodsshouldbeprefixedwithandunderscore Example private function _getUserCrew() DBcollectionname&Variablenameshouldalwaysbelowercase.Multiplewords shouldbeseparatedbyunderscore. Incorrect $usercrew $userCrew $User_crew Correct $user_crew

Comparing boolean values


Alwaysuse===and!==whencomparingTRUEorFALSEresult.Areturnvalueof or0wouldbeevaluatetoFALSEinloosecomparisons.

Typecasting
AlwaystypecastnumericvaluesbeforeUpdating/InsertingintotheMongoDB. ThisistoavoidsavingInteger/FloatvalueasStringinDB,whichwillhaveproblem whenyoudoquery.

DRY - Dont Repeat Yourself


Donotcopypastethesamepieceofcodeoverandoveragain.Instead,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. Keydescribewhatyouaregetting.Example:user_crew,fruit_skill,fruit_exp,quest_area... IDtheassociatedIDfromDB. InfullyourMCkeymaylooklikethat: ps_user_crew_[user_id]>storingtheuserscrewdbdata. PleasedefineMCkeyasconstantinaseparatedconfigfile.Thisistomakesureyouknowwhat 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

Potrebbero piacerti anche