Home > Backend Development > PHP Tutorial > Detailed explanation of php using PHPstorm's automatic prompts

Detailed explanation of php using PHPstorm's automatic prompts

黄舟
Release: 2023-03-15 12:32:01
Original
1991 people have browsed it

This article mainly introduces the detailed explanation of how PHP can make better use of PHPstorm's automatic prompts. It has certain reference value. Those who are interested can learn more

Instructions

After writing Java for a period of time, I am particularly not used to the weak typing method of PHP itself. I always feel uneasy when writing code. Especially PHP itself is a weakly typed language, so when coding, Many times there is no code prompt.

A general example


class Data {
  public $name;
  public $gender;
  public $age;
  public function __construct($name,$gender,$age) {
    $this->name = $name;
    $this->gender = $gender;
    $this->age = $age;
  }
}
class Test {
  public function run() {
    $data = [
      new Data('张三','男',18),
      new Data('李四','男',14),
      new Data('王五','男',17),
      new Data('大姨妈','女',23),
    ];
  }
  private function eachData($data) {
    foreach($data as $item) {
      echo $item->name.'=>'.$item->gender.'=>'.$item->age."\n";
    }
  }
}
(new Test)->run();
Copy after login

Judging from the above examples, generally speaking, there is no problem, but when writing


cho $item->name.'=>'.$item->sex.'=>'.$item->age."\n";
Copy after login

In this code, there is no automatic prompt when calling attributes. If the amount of data is large, you need to scroll up and copy or write it down to reduce the coding speed. , and sometimes I don’t know the composition in my mind, and I’m afraid I’ll make a mistake.

The following is a complete example I wrote using comments and its own PHP features:


class Data {
  public $name;
  public $gender;
  public $age;
  public function __construct($name,$gender,$age) {
    $this->name = $name;
    $this->sex = $gender;
    $this->age = $age;
  }
}
class Test {
  public function run() {
    $data = [
      new Data('张三','男',18),
      new Data('李四','男',14),
      new Data('王五','男',17),
      new Data('大姨妈','女',23),
    ];
  }
  /**
   * 遍历输出数据
   * @param array $data
   */
  private function eachData($data) {
    foreach($data as $item) {
      if($item instanceof Data) {
        echo $item->name.'=>'.$item->gender.'=>'.$item->age."\n";
      }
    }
  }
}
(new Test)->run();
Copy after login

The main thing here is to add an if judgment and judge Whether the data type is a specific instance of Data;

In this place, PHPstorm will automatically prompt when calling the $item attribute based on this judgment, which is very convenient.

Thinking

Some thoughts I got from here is that we can better consider rigor when writing programs. From the above example Look, if you do this and add some error handling mechanisms, you can better ensure the security and integrity of the data, not just the convenience of editor prompts.

When you do code inspection and tracking later, it will be a very convenient thing, and the business logic will be clearer.

The above is the detailed content of Detailed explanation of php using PHPstorm's automatic prompts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template