PHP 良好實踐 (Best Practice)

41
PHP PIXNET 踢克⼤⼩事分享 PIXNET @ Win 17/11/10 kylinyu.win 良好實踐

Transcript of PHP 良好實踐 (Best Practice)

  1. 1. PHP PIXNET PIXNET @ Win 17/11/10 kylinyu.win
  2. 2.
  3. 3.
  4. 4. Framework Coding Style PIXNET Platform
  5. 5. Naming // ResultSet / Collection $users = User::search(1); // Row $article = BlogArticle::find(1);
  6. 6. foreach ($employees as $employee) { $expectedSalary = $employee->calculateExpectedSalary(); $experience = $employee->getExperience(); $githubLink = $employee->getGithubLink(); $data = [ $expectedSalary, $experience, $githubLink ]; render($data); }
  7. 7. foreach ($employees as $employee) { $expectedSalary = $employee->calculateExpectedSalary(); $experience = $employee->getExperience(); $githubLink = $employee->getGithubLink(); $data = [ $expectedSalary, $experience, $githubLink ]; render($data); } foreach ($employees as $employee) { render([ $employee->calculateExpectedSalary() $employee->getExperience(), $employee->getGithubLink(), ]); }
  8. 8. foreach ($employees as $employee) { render([ $employee->calculateExpectedSalary() $employee->getExperience(), $employee->getGithubLink(), ]); } $bouns = 1.25; foreach ($employees as $employee) { $expectedSalary = $employee->calculateExpectedSalary() * $bouns; render([ $expectedSalary, $employee->getExperience(), $employee->getGithubLink(), ]); } ()
  9. 9. $user = User::find(1); if (!$user->friends->friends_feeds['12345']->friends_comments->count()) { echo 'No comments.' }
  10. 10. $user = User::find(1); $friends_comments_count = $user->friends->friends_feeds['12345']->friends_comments->count(); if (!$friends_comments_count) { echo 'No comments.' } $user = User::find(1); if (!$user->friends->friends_feeds['12345']->friends_comments->count()) { echo 'No comments.' }
  11. 11. class CardAuthor { // .... public function checkAdmin() { return (2 === $this->role); } }
  12. 12. class CardAuthor { // .... public function checkAdmin() { return (2 === $this->role); } } class CardAuthor { // .... const ROLE_GENERAL = 0; // const ROLE_EDITOR = 1; // const ROLE_ADMIN = 2; // public function checkAdmin() { return (self::ROLE_EDITOR === $this->role); } }
  13. 13. / $member = Member::find(1); if (5 < $member->day_logs) { throw new Exception('!'); } ($variable) (constant)
  14. 14. $member = Member::find(1); if (5 < $member->day_logs) { throw new Exception('!'); } $workdays_per_week_limit = 5; if ($workdays_per_week_limit < $member->day_logs) { throw new Exception('!'); } / ($variable) (constant)
  15. 15. - public function checkAlbumStatus() { if ('ok' === $this->album->status) { return true; } return false; }
  16. 16. - public function checkAlbumStatus() { if ('ok' === $this->album->status) { return true; } return false; } public function checkAlbumStatus() { $album_status = $this->album->status; return ('ok' === $album_status) ? true : false; }
  17. 17. - public function checkAlbumStatus() { $is_ok = ('ok' === $this->album->status); return $is_ok; }
  18. 18. - public function checkAlbumStatus() { $is_ok = ('ok' === $this->album->status); return $is_ok; } public function checkAlbumStatus() { return ('ok' === $this->album->status); }
  19. 19. set property get property is/has boolean check boolean, void filter , input/output mixed validate boolean, void, throw exception - can should
  20. 20. - /** * getUser * * @param int $id * @return UserRow */ function getUser($id) { // implement } /** * setAge * * @param UserRow * @return boolean */ function setAge($user) { // implement } /** * isAdmin * * @param UserRow * @return boolean */ function isAdmin($user) { // implement } /** * checkLogin * * @return void */ function checkFreeExpired() { // implement } set get is check
  21. 21. - /** * validateCSRFToken CSTF Token * * @thorw Exception * @return void */ function validateCSRFToken() { if (...) { throw new Exception("Don't Hack Me."); } // return true; //(optional) }