Share
Embed
開發現代化 PHP 套件:從零開始
隨著 PHP 7 的問世,以及越來越多人提倡 PHP: The Right Way 等現代化概念,如導入物件導向程式設計,設計模型還有軟體工程等概念。
以及符合許多現代化 PHP 的框架也是如雨後春筍般的冒出,本議程,是要帶領聽眾透過開發簡易 PHP 套件學習與邁向現代化 PHP 開發之路。
peter279k
March 24, 2019
Transcript
51 slides
-
Outline Outline 自我介紹 自我介紹 緣起 緣起 PHP 7.1+ 的新特性 PHP
7.1+ 的新特性 開發一個新套件 開發一個新套件 Live demo Live demo -
自我介紹 自我介紹 Peter 目前在 ITRI 服務 後端開發者 3+ years PHP
後端程式開發 PHP 5.3 → PHP 7+ 會一點 Python No framework→ Slim→ Laravel 2 years 開源專案貢獻者 GitHub -
有一天呢 有一天呢 PM:XXX 要請假,OOO 要離職 PM:XXX 要請假,OOO 要離職 PM:你要去接 OOXX
平台 PM:你要去接 OOXX 平台 我:恩好 我:恩好 -
OOXX 平台 OOXX 平台 Laravel 5.2 Laravel 5.2 Ubuntu 14.04
Ubuntu 14.04 PHP 5.5.9 PHP 5.5.9 -
line of source code line of source code find .
-name '*.php' | xargs -
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 -
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]; ...... -
這專案到底有什麼問題? 這專案到底有什麼問題? MVC 的 pattern 不見了 MVC 的 pattern 不見了
用了框架 !== 變潮 用了框架 !== 變潮 Coding style → Free style Coding style → Free style 上線環境過度的老舊 上線環境過度的老舊 -
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 -
建置 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 -
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 參考資料 參考資料 前置環境 前置環境 -
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 -
Single Responsibility Single Responsibility A Class should be responsible for
a single task. A Class should be responsible for a single task. -
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); } -
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. -
Open-Close Principle Open-Close Principle interface LoginInterface { public function authUser(
$user); } class NormalLogin implements LoginInter { public function authUser( $user) { // auth logic... } } -
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); } } -
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. -
Liskov Substitution Liskov Substitution class Rectangle { protected $width; protected
$height; public function setHeight( $height) { $this->height = $height; } public function getHeight() { return $this->height; } -
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; } } -
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; } -
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. -
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'; } } -
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(); } } -
Dependency Inversion Dependency Inversion Depend on abstractions, not on concretions.
Depend on abstractions, not on concretions. -
Dependency Inversion Dependency Inversion interface Mailer { public function send();
} class SmtpMailer implements Mailer { public function send() { } } -
Dependency Inversion Dependency Inversion class SendWelcomeMessage { private $mailer; public
function __construct(Mailer $mailer) { $this->mailer = $mailer; } } class SendGridMailer implements Mailer { public function send() { } } -
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. -
軟體開發流程心法 軟體開發流程心法 分析 → 設計 → 實做 → 測試 分析
→ 設計 → 實做 → 測試 -
分析 分析 PM : 我需要一個兩個整數相加減乘除的功能 PM : 我需要一個兩個整數相加減乘除的功能 整數可能會很大 GMP
(sudo apt-get install php-gmp) BcMath (sudo apt-get install php-bcmath) -
設計 設計 $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'); -
實做 實做 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 } -
測試 測試 Doing unit test with PHPUnit Doing unit test
with PHPUnit 設計測試案例 設計測試案例