Secure PHP Development- P15
Số trang: 5
Loại file: pdf
Dung lượng: 96.89 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Secure PHP Development- P15: Welcome to Secure PHP Development: Building 50 Practical Applications. PHPhas come a long way since its first incarnation as a Perl script. Now PHP is a powerfulWeb scripting language with object-oriented programming support. Slowlybut steadily it has entered the non-Web scripting arena often reserved for Perl andother shell scripting languages. Arguably, PHP is one of the most popular Web platforms.
Nội dung trích xuất từ tài liệu:
Secure PHP Development- P15Chapter 3PHP Best PracticesIN THIS CHAPTER ◆ Best practices for naming variables and functions or methods ◆ Best practices for functions or methods ◆ Best practices for database ◆ Best practices for user interface ◆ Best practices for documentation ◆ Best practices for configuration managementTHE APPLICATION CODE PRESENTED in this book uses a set of programming practicesthat qualify as best practices for any PHP application development. This chapterdiscusses these practices. Familiarizing yourself with them will ease the learningcurve for the applications discussed in the rest of the book.Best Practices for Naming Variablesand FunctionsTop software engineers know that good variable, function (or method), and classnames are necessary for the maintainability of the code. A good name is one thatconveys meaning related to the named function, object, class, variable, etc.Application code becomes very difficult to understand if the developers don’t usegood, meaningful names. Take a look at the following code sample:42 Part I: Designing PHP Applications function outputDisplayMsg($outTextMsgData = null) { echo $outTextMsgData; } ?> Now look at the same code segment with meaningful names for variables and functions: The second version is clearly easier to understand because showMessage is a bet- ter name for the outputDisplayMsg function. Now let’s look at how you can use easy-to-understand names for variables, functions (or methods), and classes. When creating a new variable or function name (or method), ask yourself the following questions: ◆ What is the purpose of this variable? In other words, what does this vari- able hold? ◆ Can you use a descriptive name that represents the data the variable holds? ◆ If the descriptive name appears to be too long, can you use meaningful abbreviations? For example, $textMessage is as good as $txtMsg. Names exceeding 15 characters probably need to be reconsidered for abbreviation. Chapter 3: PHP Best Practices 43 After you determine a name, follow these rules: ◆ Use title casing for each word in multiword names. However, the very first word should be lowercase. For example, $msgBody is a better name then $msgbody, $messageBODY, or $message_body. Single word names should be kept in lowercase. For example, $path and $data are single word variables. ◆ Use all capital letters to name variables that are “constant like” — in other words, variables that do not change within the application. For example, if you read a variable from a configuration file, the name of the variable can be in all uppercase. To separate uppercase words, use under- score character (for example, use $TEMPLATE_DIR instead of $TEMPLATE- DIR). However, when creating constants it is best to use define() function. For example, define(PI, 3.14) is preferred over $PI = 3.14. The defined constant PI cannot be changed once defined whereas $PI can be changed. ◆ Use verbs such as get, set, add, delete, modify, update, and so forth in naming your function or method. For example, getSomething(), setSomething(), and modifySomething() are better function names than accessSomething(), storeSomething(), and editSomething(), respectively.Best Practices for Function/MethodIn this section I discuss a set of practices that will improve your function or methodcode.Returning arrays with careWhen your function (or method) returns an array, you need to ensure that thereturn value is a defined array because the code from which the function is calledis expecting an array. For example, review the following bad code segment.// BADfunction getData(){ $stmt = “SELECT ID, myField1, myField2 from myTable”; $result = $this->dbi->query($stmt);44 Part I: Designing PHP Applications if ($result != NULL) { while($row = $result->fetchRow()) { $retArray[$row->ID] = $row; } } return $retArray; } In this example, the function called getData() returns an array called $retArray when the SQL statement executed returns one or more rows. The func- tion works fine if the SQL select statement always returns at least one row. However, it returns nothing when the SQL statement returns no rows. In such a case, the following code segment, which calls the function, produces a PHP warn- ing message: error_reporting(E_AL ...
Nội dung trích xuất từ tài liệu:
Secure PHP Development- P15Chapter 3PHP Best PracticesIN THIS CHAPTER ◆ Best practices for naming variables and functions or methods ◆ Best practices for functions or methods ◆ Best practices for database ◆ Best practices for user interface ◆ Best practices for documentation ◆ Best practices for configuration managementTHE APPLICATION CODE PRESENTED in this book uses a set of programming practicesthat qualify as best practices for any PHP application development. This chapterdiscusses these practices. Familiarizing yourself with them will ease the learningcurve for the applications discussed in the rest of the book.Best Practices for Naming Variablesand FunctionsTop software engineers know that good variable, function (or method), and classnames are necessary for the maintainability of the code. A good name is one thatconveys meaning related to the named function, object, class, variable, etc.Application code becomes very difficult to understand if the developers don’t usegood, meaningful names. Take a look at the following code sample:42 Part I: Designing PHP Applications function outputDisplayMsg($outTextMsgData = null) { echo $outTextMsgData; } ?> Now look at the same code segment with meaningful names for variables and functions: The second version is clearly easier to understand because showMessage is a bet- ter name for the outputDisplayMsg function. Now let’s look at how you can use easy-to-understand names for variables, functions (or methods), and classes. When creating a new variable or function name (or method), ask yourself the following questions: ◆ What is the purpose of this variable? In other words, what does this vari- able hold? ◆ Can you use a descriptive name that represents the data the variable holds? ◆ If the descriptive name appears to be too long, can you use meaningful abbreviations? For example, $textMessage is as good as $txtMsg. Names exceeding 15 characters probably need to be reconsidered for abbreviation. Chapter 3: PHP Best Practices 43 After you determine a name, follow these rules: ◆ Use title casing for each word in multiword names. However, the very first word should be lowercase. For example, $msgBody is a better name then $msgbody, $messageBODY, or $message_body. Single word names should be kept in lowercase. For example, $path and $data are single word variables. ◆ Use all capital letters to name variables that are “constant like” — in other words, variables that do not change within the application. For example, if you read a variable from a configuration file, the name of the variable can be in all uppercase. To separate uppercase words, use under- score character (for example, use $TEMPLATE_DIR instead of $TEMPLATE- DIR). However, when creating constants it is best to use define() function. For example, define(PI, 3.14) is preferred over $PI = 3.14. The defined constant PI cannot be changed once defined whereas $PI can be changed. ◆ Use verbs such as get, set, add, delete, modify, update, and so forth in naming your function or method. For example, getSomething(), setSomething(), and modifySomething() are better function names than accessSomething(), storeSomething(), and editSomething(), respectively.Best Practices for Function/MethodIn this section I discuss a set of practices that will improve your function or methodcode.Returning arrays with careWhen your function (or method) returns an array, you need to ensure that thereturn value is a defined array because the code from which the function is calledis expecting an array. For example, review the following bad code segment.// BADfunction getData(){ $stmt = “SELECT ID, myField1, myField2 from myTable”; $result = $this->dbi->query($stmt);44 Part I: Designing PHP Applications if ($result != NULL) { while($row = $result->fetchRow()) { $retArray[$row->ID] = $row; } } return $retArray; } In this example, the function called getData() returns an array called $retArray when the SQL statement executed returns one or more rows. The func- tion works fine if the SQL select statement always returns at least one row. However, it returns nothing when the SQL statement returns no rows. In such a case, the following code segment, which calls the function, produces a PHP warn- ing message: error_reporting(E_AL ...
Tìm kiếm theo từ khóa liên quan:
lập trình web với PHP Lập trình php các framework phổ biến Ngôn ngữ lập trình php Giới thiệu về MysqlTài liệu có liên quan:
-
66 trang 159 0 0
-
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 138 0 0 -
47 trang 118 2 0
-
Tạo mạng xã hội với PHP - part 43
10 trang 47 0 0 -
Bài giảng Lập trình web nâng cao: Chương 1 - Trường ĐH Văn Hiến
16 trang 43 1 0 -
Bài giảng Lập trình Web: Chương 2 - Ths. Trần Phi Hảo
54 trang 40 0 0 -
PHP: The Good Parts: Delivering the Best of PHP- P5
20 trang 39 0 0 -
24 trang 38 0 0
-
TUTORIAL JOOMLA: VirtueMart Component - Thêm danh mục sản phẩm
6 trang 34 0 0 -
Professional VB 2005 - 2006 phần 6
110 trang 32 0 0
Tài liệu mới:
-
14 trang 1 0 0
-
Đề thi giữa học kì 2 môn Tin học lớp 11 năm 2022-2023 - Trường THPT Vĩnh Bảo, Hải Phòng
3 trang 0 0 0 -
3 trang 1 0 0
-
giáo án vật lý 11 - định luật ôm đối với các loại mạch điện
5 trang 1 0 0 -
Đề thi thử THPT Quốc gia năm học 2017 - 2018 môn Toán - Trường THPT Chuyên Bắc Ninh - Mã đề 601
6 trang 1 0 0