@@ -175,12 +175,11 @@ saveCityZipCode($matches['city'], $matches['zipCode']);
175175
176176** [ ⬆ 返回顶部] ( #目录 ) **
177177
178- ### Avoid nesting too deeply and return early
178+ ### 避免深层潜逃,尽早返回
179179
180- Too many if else statemetns can make your code hard to follow. Explicit is better
181- than implicit.
180+ 太多的if else语句通常会是你的代码难以阅读,直白优于隐晦
182181
183- ** Bad :**
182+ ** 糟糕 :**
184183
185184``` php
186185function isShopOpen($day)
@@ -206,7 +205,7 @@ function isShopOpen($day)
206205}
207206```
208207
209- ** Good :**
208+ ** 好的 :**
210209
211210``` php
212211function isShopOpen($day)
@@ -223,7 +222,7 @@ function isShopOpen($day)
223222}
224223```
225224
226- ** Bad :**
225+ ** 糟糕的 :**
227226
228227``` php
229228function fibonacci($n)
@@ -244,7 +243,7 @@ function fibonacci($n)
244243}
245244```
246245
247- ** Good :**
246+ ** 好的 :**
248247
249248``` php
250249function fibonacci($n)
@@ -753,9 +752,9 @@ And now you must use instance of `Configuration` in your application.
753752
754753** [ ⬆ 返回顶部] ( #目录 ) **
755754
756- ### Don't use a Singleton pattern
755+ ### 不要使用单例模式
757756
758- Singleton is a [ anti-pattern] ( https://en.wikipedia.org/wiki/Singleton_pattern ) .
757+ 单例是一种 [ anti-pattern] ( https://en.wikipedia.org/wiki/Singleton_pattern ) .
759758
760759** 坏:**
761760
@@ -1019,26 +1018,29 @@ inventoryTracker('apples', $request, 'www.inventory-awesome.io');
10191018** [ ⬆ 返回顶部] ( #目录 ) **
10201019
10211020
1022- ## Objects and Data Structures
1021+ ## 对象和数据结构
10231022
1024- ### Use getters and setters
1023+ ### 使用 getters 和 setters
1024+ 在PHP中你可以对方法使用` public ` , ` protected ` , ` private ` 来控制对象属性的变更
1025+ * 当你想对对象属性做获取之外的操作时,你不需要在代码中去寻找并修改每一个该属性访问方法
10251026
1026- In PHP you can set ` public ` , ` protected ` and ` private ` keywords for methods.
1027- Using it, you can control properties modification on an object.
1028-
1029- * When you want to do more beyond getting an object property, you don't have
1030- to look up and change every accessor in your codebase.
10311027* Makes adding validation simple when doing a ` set ` .
1028+ * 当有` set ` 对应的属性方法时,易于增加参数的验证
10321029* Encapsulates the internal representation.
1030+ * 封装内部的表示
10331031* Easy to add logging and error handling when getting and setting.
1032+ * 使用set* 和get* 时,易于增加日志和错误控制
10341033* Inheriting this class, you can override default functionality.
1034+ * 继承当前类时,可以复写默认的方法功能
10351035* You can lazy load your object's properties, let's say getting it from a
10361036server.
1037+ * 当对象属性是从其他服务获取时,get* ,set* 易于使用延迟加载
10371038
10381039Additionally, this is part of Open/Closed principle, from object-oriented
10391040design principles.
1041+ 此外,这样的方式也符合OOP开发中的开闭原则
10401042
1041- ** 坏 :**
1043+ ** 糟糕 :**
10421044
10431045``` php
10441046class BankAccount
@@ -1096,8 +1098,9 @@ $balance = $bankAccount->getBalance();
10961098** [ ⬆ 返回顶部] ( #目录 ) **
10971099
10981100### Make objects have private/protected members
1101+ 对象属性多使用private/protected 限定
10991102
1100- ** 坏 :**
1103+ ** 糟糕 :**
11011104
11021105``` php
11031106class Employee
@@ -1138,17 +1141,15 @@ echo 'Employee name: '.$employee->getName(); // Employee name: John Doe
11381141
11391142** [ ⬆ 返回顶部] ( #目录 ) **
11401143
1141- ## Classes
1144+ ## 类
11421145
1143- ### Use method chaining
1146+ ### 使用方法链
11441147
1145- This pattern is very useful and commonly used in many libraries such
1146- as PHPUnit and Doctrine. It allows your code to be expressive, and less verbose.
1147- For that reason, use method chaining and take a look at how clean your code
1148- will be. In your class functions, simply use ` return $this ` at the end of every ` set ` function,
1149- and you can chain further class methods onto it.
1148+ 这是一种非常有用的,并且在其他类库中(PHPUnit 和 Doctrine)常用的模式
1149+ 它使你的代码更有表达力,减少冗余
1150+ 因为这个原因,来看看如何使用方法链来使你的代码变得清爽:在你的类的每一个` set ` 方法的最后简单的使用 ` return $this ` ,然后进一步将类方法链起来
11501151
1151- ** Bad :**
1152+ ** 糟糕的 :**
11521153
11531154``` php
11541155class Car
@@ -1185,7 +1186,7 @@ $car->setModel('F-150');
11851186$car->dump();
11861187```
11871188
1188- ** Good :**
1189+ ** 好的 :**
11891190
11901191``` php
11911192class Car
@@ -1197,7 +1198,7 @@ class Car
11971198 public function setMake($make)
11981199 {
11991200 $this->make = $make;
1200-
1201+
12011202 // NOTE: Returning this for chaining
12021203 return $this;
12031204 }
@@ -1233,26 +1234,19 @@ $car = (new Car())
12331234
12341235** [ ⬆ back to top] ( #table-of-contents ) **
12351236
1236- ### Prefer composition over inheritance
1237+ ### 组合优于基础
12371238
1238- As stated famously in [ * Design Patterns* ] ( https://en.wikipedia.org/wiki/Design_Patterns ) by the Gang of Four,
1239- you should prefer composition over inheritance where you can. There are lots of
1240- good reasons to use inheritance and lots of good reasons to use composition.
1241- The main point for this maxim is that if your mind instinctively goes for
1242- inheritance, try to think if composition could model your problem better. In some
1243- cases it can.
1239+ 正如之前所说[ * Design Patterns* ] ( https://en.wikipedia.org/wiki/Design_Patterns ) the Gang of Four 所著,
1240+ 我们应该尽量优先选择组合而不是继承的方式。使用继承和组合都有很多好处。
1241+ 这个准则的主要意义在于当你本能的使用继承时,试着思考一下` 组合 ` 是否能更好对你的需求建模。
1242+ 在一些情况下,是这样的。
12441243
1245- You might be wondering then, "when should I use inheritance?" It
1246- depends on your problem at hand, but this is a decent list of when inheritance
1247- makes more sense than composition:
1244+ 接下来你或许会想,“那我应该在什么时候使用继承?” 答案依赖于你的问题,当然下面有一些何时继承比组合更好的说明:
1245+ 1 . 你的继承表达了“是一个”而不是“有一个”的关系(人类-》动物,用户-》用户详情)
1246+ 2 . 你可以复用基类的代码(人类可以像动物一样移动)
1247+ 3 . 你想通过修改基类对所有派生类做全局的修改(当动物移动时,修改她们的能量消耗)
12481248
1249- 1 . Your inheritance represents an "is-a" relationship and not a "has-a"
1250- relationship (Human->Animal vs. User->UserDetails).
1251- 2 . You can reuse code from the base classes (Humans can move like all animals).
1252- 3 . You want to make global changes to derived classes by changing a base class.
1253- (Change the caloric expenditure of all animals when they move).
1254-
1255- ** Bad:**
1249+ ** 糟糕的:**
12561250
12571251``` php
12581252class Employee
@@ -1269,10 +1263,11 @@ class Employee
12691263 // ...
12701264}
12711265
1272- // Bad because Employees "have" tax data.
1273- // EmployeeTaxData is not a type of Employee
12741266
1275- class EmployeeTaxData extends Employee
1267+ // Employees "有" taxdata,EmployeeTaxData不是一种Employee,使用集成很糟糕
1268+
1269+
1270+ class EmployeeTaxData extends Employee
12761271{
12771272 private $ssn;
12781273 private $salary;
@@ -1331,13 +1326,13 @@ class Employee
13311326
13321327## SOLID
13331328
1334- ** SOLID** is the mnemonic acronym introduced by Michael Feathers for the first five principles named by Robert Martin, which meant five basic principles of object-oriented programming and design.
1329+ ** SOLID** 是Michael Feathers推荐的便于记忆的首字母简写,它代表了Robert Martin命名的最重要的五个面对对象编码设计原则
1330+ * [ S: 指责单一原则 (SRP)] ( #single-responsibility-principle-srp )
1331+ * [ O: 开闭原则原则 (OCP)] ( #openclosed-principle-ocp )
1332+ * [ L: 里氏替换原则 (LSP)] ( #liskov-substitution-principle-lsp )
1333+ * [ I: 接口隔离原则 (ISP)] ( #interface-segregation-principle-isp )
1334+ * [ D: 依赖反转原则 (DIP)] ( #dependency-inversion-principle-dip )
13351335
1336- * [ S: Single Responsibility Principle (SRP)] ( #single-responsibility-principle-srp )
1337- * [ O: Open/Closed Principle (OCP)] ( #openclosed-principle-ocp )
1338- * [ L: Liskov Substitution Principle (LSP)] ( #liskov-substitution-principle-lsp )
1339- * [ I: Interface Segregation Principle (ISP)] ( #interface-segregation-principle-isp )
1340- * [ D: Dependency Inversion Principle (DIP)] ( #dependency-inversion-principle-dip )
13411336
13421337### Single Responsibility Principle (SRP)
13431338
0 commit comments