search
HomeBackend DevelopmentPHP TutorialPHP skills: how to cleverly avoid some bad codes in PHP programs

This article shares with you how to find bad code in PHP example code and how to fix it. Interested friends can refer to it.

I have been doing PHP development for almost a year. During this year, I learned a lot of techniques in the production environment and learned a lot of things. During this period, I also read some excellent source codes and information about Code book, I have some thoughts on writing code, and I have seen a lot of good and bad code written by others. Let me talk about my own insights and improvements here.

This blog expresses my own feelings. When writing code, I set rules for myself, which can make the code clear and readable and avoid pitfalls. Although these simple rules may not look as exciting as design patterns, with regular attention they can make your code look cleaner.

1. Do not use undeclared variables outside the object

This problem may not be easy to understand when expressed. This problem is determined by the characteristics of the PHP language itself. Since PHP is a weakly typed dynamic scripting language, in many cases, this language is given very loose conditions for developers to write code. But often these conveniences can also turn into pitfalls, so you should pay special attention when using some convenient writing methods in dynamic languages.

Let’s declare a class first. Let’s call this class the user class. The background setting of this User class is that it comes with the framework and is not allowed to be modified. It is hidden deep in the framework and is not easy to find. In fact, For examples, please refer to the Request class of the laravel framework. The code is as follows:

class User {
  public $username;
  public $password;
  
  public $otherInfo = [];
  
  
  public function readUserInfo() {
    return [
      'username' => $this->username,
      'password' => $this->password,
    ];
  }
  
  public function addAtri($info) {
    array_push($this->otherInfo, $info);
  }
}

Such code seems to be quite satisfactory, but next, we need to operate this class:

$user = new User();
$user->userRealName = "hello world";

Such code can be run completely in PHP and will not report an error, but such code will interfere with some subsequent things. We now assume that the above code is an interceptor in the PHP web project, or it can be called middleware. Then we will use the instance of this class in the controller and use the variable added in the middleware. , as follows:

 class WebOperate {
   public function doOprate(User $user) {
     $user->userRealName = "hello world";
     next($user);
   }
 }

The scenario set here is that WebOperate is a middleware, and all Controllers will reach the Controller after passing through this middleware. After that, in Process the corresponding Controller function. Next, the Controller will inject the middleware instance for the controller to use, but the middleware developer does not care much about its existence:

 class IndexController {
   public function index(User $user) {
     return $user->userRealName;
   }
 }

And such code can run perfectly. Next, the developer wants to implement another User class and add some other functions to this User class. As mentioned before, this class is deep in the framework. And it is difficult to find, and modification is not allowed, because other functions use this class, so we can only inherit and add methods. According to development experience, developers will think that the userRealName variable exists in the User class, so this is the writing method:

The first is the Teacher class derived based on this User:

 class Teacher extends User {
   public function sayHello() {
     return "hello world";
   }
 }

In this way, our Teacher can say hello, but at this time, we still want to know the teacher’s real name in our Controller, what should we do? Based on experience, we can change the injected class to Teacher and return the real name:

 class IndexController {
   public function index(Teacher $user) {
     return $user->userRealName;
   }
 }

Then the question arises. In fact, there is no such thing in the User class. class, so this variable has no value at all, but according to experience, the middleware has already been assigned a value once, so we should be able to use it directly, but there is no such value. We started looking at the source code and found that this does not exist in the inherited User class. Variable, then why can this variable be used before? Because in the middleware, the user's strength is paid.

So we cannot directly use undeclared variables in a class.

We should write like this:

class WebOperate {
  public function doOprate(User $user) {
    $user->addAtri([
      'userRealName' => 'hello world',
    ]);
    next($user);
  }
}

Such middleware can also use the same method when calling inherited classes. It is very simple. And it is very difficult to produce bad taste.

2. Class or array

In fact, this problem also derives another problem, which is the problem of function return value.

First of all, I make it clear that I personally feel that it is not good for a function to return multiple types of values. Although it is very common in dynamic languages, many native methods of PHP also have this, but in Using this method in production will cause uncertainty in function returns. We need to make a lot of judgments to prove our conclusions. However, if there is only one return value type, we can directly judge the return value.

is like the following code:

public function addNewUser() {
    $res = $this->addData();
    if ($res) {
      return true;
    } else {
      return [
        'error' => 1,
        'errormsg' => "没有添加成功"
      ];
    }
  }

Such code will often be judged one more time by the caller, as follows:

public function index() {
    $res = $this->addNewUser();
    if (is_array($res) && isset($res['error'])) {
      return isset($res['errormsg']) ? $res['errormsg'] : "未知错误";
    }
    return "成功";
  }

This kind of code will appear almost every time this function is called. Not only is the code unsightly, but it is also very bloated.

Such code needs to be improved. First, limit the return value of the function. For example, we only let this function return a bool type number:

public function addNewUser() {
  $res = $this->addData();
  if ($res) {
    return true;
  } else {
    return false;
  }
}

但是,显然,很多时候,我们要的不是简单的真价值,所以,我们会选择返回更多信息,这个时候,我们可以有三种处理方式。

1)返回int类型的数,然后通过这个int类型的数去判断处理结果,我们可以添加上映射关系:

class Operate{
  public $operateRes = [
    0 => '成功',
    1 => '添加失败',
    2 => '未知错误',
  ];
  
  
  public function addNewUser() {
    $res = $this->addData();
    if ($res) {
      return 0;
    } else if ($res > 1) {
      return 1;
    }
    return 2;
  }
  
}

这样方法的调用者就可以很简单的使用方法并给出提示了:

$opera = new Operate();
$res = $opera->addNewUser();
return $opera->operateRes[$res];

给出统一的返回值类型的时候就完全不需要判断返回值类型而且可以设置一个规范返回提示。

2)我们也可以使用数组

3)数组给人不缺定性,因为很多时候,数组里可以认为的少写一些元素,如果少写了,程序直接报错,很不好。

所以第三种方式就是建议将固定格式的返回,写成一个类,做返回的时候,使用这个类:

class Operate{
  public function addNewUser() {
    $res = $this->addData();
    $result = new Result();
    if ($res) {
      $result->errno = 0;
      $result->errmsg = "成功";
    } else if ($res > 1) {
      $result->errno = 1;
      $result->errmsg = "失败";
    }
    $result->errno = 2;
    $result->errmsg = "未知错误";
    return $result;
  }
  
}

class Result {
  public $errno;
  public $errmsg;
}

这样的返回,保证了所有变量的存在,同样可以减少一次判断。

所以,综合以上,在我们返回结果的时候,尽量使用同种类型的变量,尽量减少使用数组返回。

The above is the detailed content of PHP skills: how to cleverly avoid some bad codes in PHP programs. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How can you prevent session fixation attacks?How can you prevent session fixation attacks?Apr 28, 2025 am 12:25 AM

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

How do you implement sessionless authentication?How do you implement sessionless authentication?Apr 28, 2025 am 12:24 AM

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

What are some common security risks associated with PHP sessions?What are some common security risks associated with PHP sessions?Apr 28, 2025 am 12:24 AM

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

How do you destroy a PHP session?How do you destroy a PHP session?Apr 28, 2025 am 12:16 AM

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How can you change the default session save path in PHP?How can you change the default session save path in PHP?Apr 28, 2025 am 12:12 AM

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function