Upgrade to Pro — share decks privately, control downloads, hide ads and more …

開發現代化 PHP 套件:從零開始

開發現代化 PHP 套件:從零開始

隨著 PHP 7 的問世,以及越來越多人提倡 PHP: The Right Way 等現代化概念,如導入物件導向程式設計,設計模型還有軟體工程等概念。
以及符合許多現代化 PHP 的框架也是如雨後春筍般的冒出,本議程,是要帶領聽眾透過開發簡易 PHP 套件學習與邁向現代化 PHP 開發之路。

peter279k

March 24, 2019
Tweet

More Decks by peter279k

Other Decks in Programming

Transcript

  1. Outline Outline 自我介紹 自我介紹 緣起 緣起 PHP 7.1+ 的新特性 PHP

    7.1+ 的新特性 開發一個新套件 開發一個新套件 Live demo Live demo
  2. 自我介紹 自我介紹 Peter 目前在 ITRI 服務 後端開發者 3+ years PHP

    後端程式開發 PHP 5.3 → PHP 7+ 會一點 Python No framework→ Slim→ Laravel 2 years 開源專案貢獻者 GitHub
  3. WOW WOW 47 ./xxxxController.php 32 ./Auth/xxxxxxxContro 73 ./Auth/xxxxxControll 117 ./xxxxxController.ph

    928 ./xxxxxController.ph 204 ./xxxxController.php 217 ./xxxxxController.ph 73 ./xxxxxxxxController 13 ./Controller.php 260 ./xxxxxpController 12 ./xxxxxController.p 216 ./xxxxxController.p 43 ./xxxxxController.p 1965 ./xxxxxxController 362 ./xxxxxController.p 264 ./xxxxxxController 409 ./District/xxxxxxCo 727 ./xxxxxxController 2753 ./xxxxxxController 41 ./xxxxxxController
  4. Controllers 裡面到底? Controllers 裡面到底? public function getAminInfo(Request $request) { $adminId

    = Session::get( 'admin.id'); $isManager = Session::get( 'admin.alarm $isExtended = Session::get( 'admin.exte $sql= 'SELECT admin.* FROM admin WHERE id $admins = DB::select( $sql, array( $adminId)); if(empty( $admins)) $admin=NULL; else $admin = $admins[0]; ......
  5. 這專案到底有什麼問題? 這專案到底有什麼問題? MVC 的 pattern 不見了 MVC 的 pattern 不見了

    用了框架 !== 變潮 用了框架 !== 變潮 Coding style → Free style Coding style → Free style 上線環境過度的老舊 上線環境過度的老舊
  6. PHP 7.1+ 帶來的特性 PHP 7.1+ 帶來的特性 function testReturn(): void function

    testReturn(): void function testReturn(): ?string function testReturn(): ?string Sodium is now a core extension Sodium is now a core extension Password hashing with Argon2 Password hashing with Argon2
  7. 建置 PHP 7.1+ 環境 建置 PHP 7.1+ 環境 Composer Composer

    Composer Composer Composer Composer , , OOP principles OOP principles Design pattern Design pattern 軟體開發流程心法 軟體開發流程心法 PSR-2 PSR-2 PSR-4 PSR-4
  8. PHP 7.1+ PHP 7.1+ Ubuntu 16.04 or 18.04 or any

    Ubuntu 16.04 or 18.04 or any Linux distro Linux distro sudo add-apt-repository sudo add-apt-repository ppa:ondrej/php -y ppa:ondrej/php -y sudo apt-get update sudo apt-get update sudo apt-get install php 7.1 sudo apt-get install php 7.1 參考資料 參考資料 前置環境 前置環境
  9. OOP principles OOP principles Single Responsibility Single Responsibility Open-Close Principle

    Open-Close Principle Liskov Substitution Liskov Substitution Interface Segregation Interface Segregation Dependency Inversion Dependency Inversion
  10. Single Responsibility Single Responsibility A Class should be responsible for

    a single task. A Class should be responsible for a single task.
  11. Single Responsibility Single Responsibility class User { protected function formatResponse(

    $data) { return [ 'name' => $data->name, 'userName' => $data->userName, ]; } protected function validator( $user) { if ( $user === '') { throw new \InvalidArgumentException( "User doesn't exist") } } protected function fetchUserInformation( $userId) { return DB::table( 'users')->findOrFail( $userId); }
  12. Open-Close Principle Open-Close Principle A Class should be open to

    extension A Class should be open to extension and close to modi cation. and close to modi cation.
  13. Open-Close Principle Open-Close Principle interface LoginInterface { public function authUser(

    $user); } class NormalLogin implements LoginInter { public function authUser( $user) { // auth logic... } }
  14. Open-Close Principle Open-Close Principle class FacebookLogin implements LoginInterface { public

    function authUser( $user) { // auth logic... } } class LoginModule { public function login(LoginInterface $user) { $user->authUser( $user); } }
  15. Liskov Substitution Liskov Substitution This principle is about subtyping and

    This principle is about subtyping and inheritance. inheritance. Derived classes must be substitutable for their Derived classes must be substitutable for their base classes. base classes.
  16. Liskov Substitution Liskov Substitution class Rectangle { protected $width; protected

    $height; public function setHeight( $height) { $this->height = $height; } public function getHeight() { return $this->height; }
  17. Liskov Substitution Liskov Substitution public function setWidth( $width) { $this->width

    = $width; } public function getWidth() { return $this->width; } public function area() { return $this->height * $this->width; } }
  18. Liskov Substitution Liskov Substitution class Square extends Rectangle { public

    function setHeight( $value) { $this->width = $value; $this->height = $value; } public function setWidth( $value) { $this->width = $value; $this->height = $value; }
  19. Interface Segregation Interface Segregation Don’t make FAT Interfaces. Don’t make

    FAT Interfaces. Many client-speci c interfaces are better than one Many client-speci c interfaces are better than one general-purpose interface. general-purpose interface.
  20. Interface Segregation Interface Segregation interface Codeable { public function c

    } interface Testable { public function t } class Programmer implements Codeable, Te { public function code() { return 'coding'; } public function test() { return 'testing in localhost'; } }
  21. Interface Segregation Interface Segregation class Tester implements Testable { public

    function test() { return 'testing in test server'; } } class ProjectManagement { public function processCode(Codeable $member) { $member->code(); } }
  22. Dependency Inversion Dependency Inversion interface Mailer { public function send();

    } class SmtpMailer implements Mailer { public function send() { } }
  23. Dependency Inversion Dependency Inversion class SendWelcomeMessage { private $mailer; public

    function __construct(Mailer $mailer) { $this->mailer = $mailer; } } class SendGridMailer implements Mailer { public function send() { } }
  24. Should I learn Should I learn Design pattern? Design pattern?

    You might manage to work as a programmer for You might manage to work as a programmer for many years without knowing about a single many years without knowing about a single pattern. pattern. They're a toolkit of tried and tested solutions to They're a toolkit of tried and tested solutions to common problems in software design. common problems in software design. It de nes a common language that you and your It de nes a common language that you and your teammates can use to communicate more teammates can use to communicate more e ciently. e ciently.
  25. 設計 設計 $gmp = new GmpCalculator(); $bcMath = new BcMathCalculator();

    $calculator = new Calculator( $gmp); $calculator->add( '11111', '22222222'); $calculator->minus( '11111', '22222222'); $calculator->mul( '11111', '22222222'); $calculator->divide( '11111', '22222222');
  26. 實做 實做 interface CalculatorInterface { public function add( $num1, $num2);

    public function minus( $num1, $num2); public function mul( $num1, $num2); public function divide( $num1, $num2); } class GmpCalculator extends Calculator { // calculation logic implementation } class BcMathCalculator extends Calculator { // calculation logic implementation } class Calculator { // calculation logic }
  27. 測試 測試 Doing unit test with PHPUnit Doing unit test

    with PHPUnit 設計測試案例 設計測試案例