Home >Backend Development >PHP Tutorial >Detailed explanation of PHP's use of PHPstorm's automatic prompt function

Detailed explanation of PHP's use of PHPstorm's automatic prompt function

小云云
小云云Original
2018-01-26 09:58:356295browse

This article mainly introduces the detailed explanation of how PHP can better utilize PHPstorm's automatic prompts. It has certain reference value. Those who are interested can learn about it. I hope it can help everyone.

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();

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


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

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();

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.

Related recommendations:

phpstorm regular matching to delete empty lines and comment lines tips sharing

How to install vue in phpstorm .js plug-in

Recommended articles about the installation and use of PhpStorm

The above is the detailed content of Detailed explanation of PHP's use of PHPstorm's automatic prompt function. 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