Home Backend Development PHP Tutorial 10 Ways to Improve PHP Developer Productivity

10 Ways to Improve PHP Developer Productivity

Mar 19, 2018 pm 03:08 PM

In development work, if you master the way of "doing things" in PHP language, you will be comfortable in using it and greatly improve development efficiency. Therefore, this article introduces you to the top 10 PHP development skills, I hope it will be helpful to you.

1. How to correctly create the Index page of a website

When creating every website, establishing the index page of the website is one of the first things to do. . If you are new to PHP, a typical approach when writing an index page is to program only the content required for the index page, and create another page for other links. However, if you want to learn a more efficient way to implement PHP programming, you can use the "index.php?page=home" mode, which is used by many websites.

2. Use Request Global Array to capture data

In fact, we have no reason to use $_GET and $_POST arrays to capture values. The global array $_REQUEST allows you to obtain a get or form request. Therefore, in most cases a more efficient code for parsing the data would look like this:

  1. $action = isset($_REQUEST[' action']) ? $_REQUEST['action'] : 0;

3. Use var_dump PHP code debugging

If you are looking for PHP debugging techniques, I must say that var_dump should be the target you are looking for. This command can meet all your needs when it comes to displaying php information. Most cases of debugging code are related to getting values ​​in PHP.

4. PHP handles the code logic, and Smarty handles the presentation layer.

Smarty is a template PHP template engine written in PHP. It is currently the most famous PHP template in the industry. One of the engines. It separates logical code and external content, providing an easy-to-manage and use method to separate PHP code logic that is originally mixed with HTML code. Simply put, the purpose is to separate PHP programmers from front-end personnel, so that programmers change the logical content of the program without affecting the page design of the front-end personnel, and front-end personnel re-modify the page without affecting the program logic of the program. This is especially important in projects involving multi-person collaboration.

5. When you really need to use global values, create a Config file

It is a bad practice to create global values ​​frequently, but sometimes the actual situation does require it. Do this. It's a good idea to use global values ​​for database table or database connection information, but don't use global values ​​frequently in your PHP code. Alternatively, a better approach is to store your global variables in a config.php file.

6. If it is not defined, access is prohibited!

If you create the page correctly, there is no reason for anyone else to access the index.php page outside of index.php or home.php. Once index.php is accessed, you can open the required page by obtaining the variables. Your index page should contain code similar to the following:

  1. define('yourPage',1);

Then, other pages should contain:

  1. if (!defined('yourPage')) die('Access Denied');

The purpose of this is to prevent direct access to your other php pages. In this way, anyone who tries to access other web pages without going through index.php will get an "Access Denied" message.

7. Create a database class

If you are doing database programming (a very common task in PHP), a good idea is to create a database class to Handle any database management functions. The sample code is as follows:

       public function dbExec($query)    
 {    
     $result = $this->db->exec($query);    
     if (PEAR::isError($result))    
         errorRedirect($result->getMessage(), true);    
     else
         return $result;    
 }
Copy after login

This function only receives a query statement and executes it. It also handles any errors that may occur. You can also include audit code here, but I prefer to use a similar audit function:

// checks if arguments given are integer values not less than 0 - has multiple arguments   
 function sanitizeInput()    
 {    
     $numargs = func_num_args();    
     $arg_list = func_get_args();    
     for ($i = 0; $i < $numargs; $i++) {    
         if (!is_numeric($arg_list[$i]) || $arg_list[$i] < 0)    
             errorRedirect("Unexpected variable value", true);    
    }    
 }
Copy after login

8. One php file to handle the input, and one class.php file to handle the specific functions

An important way to avoid cluttering the code is to redirect it to other functions for processing after obtaining user input. The principle is very simple, the php file takes whatever input we need and then redirects its execution to a function in the class file. For example, suppose there is a URL like "index.php?page=profile&action=display". The URL is retrieved by profile.php and the action is "display". Then using a simple switch function, we perform the real display function:

 require_once PROJECTROOT.&#39;libs/messages.class.php&#39;;     
    $message = new Message();     
switch ($action)   
{   
    case &#39;display&#39;:   
       $message->display();   
       break;     
...
Copy after login

As shown above, I use a message class and then start the switch check. $message is just an object used by calling functions in the class.

9. Know your SQL statements and always Sanitize them

正如我以前所提到的,任何php网站中最重要的部分有99%的可能是数据库。因此,你需要非常熟悉如何正确的使用sql。学会关联表和更多高级技术。下面我将展示一个使用MySQL的函数示例,并使用本文第7条函数进行审查。

private function getSentMessages($id)    
 {    
$this->util->sanitizeInput($id);    
    $pm_table = $GLOBALS[&#39;config&#39;][&#39;privateMsg&#39;];    
 $users = $GLOBALS[&#39;config&#39;][&#39;users&#39;];    
     $sql = "SELECT PM.*, USR.username as name_sender FROM $pm_table PM, $users USR    
         WHERE id_sender = &#39;$id&#39; AND sender_purge = FALSE AND USR.id = PM.id_receiver AND is_read = TRUE    
         ORDER BY date_sent DESC";    
 $result = $this->dbQueryAll($sql);    
    return $result;    
 }
Copy after login

首先,我们对用户输入进行检查(通过一个GET变量传递消息id),然后我们执行我们的SQL命令。注意这儿SQL的用法。你需要了解如何使用别名和关联表。

10、当你只需要一个对象时,使用单例模式

在PHP中相当常见的一种情形时,我们只需要创建一个对象一次,然后在我们的整个程序中使用它。一个很好的例子就是smarty变量,一旦被初始化后就可以在任何地方使用。这种情形的一个很好实现方案就是单例模式。示例代码如下:

function smartyObject()    
 {    
     if ($GLOBALS[&#39;config&#39;][&#39;SmartyObj&#39;] == 0)    
    {    
         $smarty = new SmartyGame();    
         $GLOBALS[&#39;config&#39;][&#39;SmartyObj&#39;] = $smarty;    
    }    
     else
         $smarty = $GLOBALS[&#39;config&#39;][&#39;SmartyObj&#39;];    
     return $smarty;    
 }
Copy after login

注意,我们拥有一个全局smarty变量(该示例中它在config.php中被初始化),如果它的值为0,我们将创建一个新smarty对象。否则,它意味着该对象已经被创建,我们只需要返回它。

The above is the detailed content of 10 Ways to Improve PHP Developer Productivity. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles