Home  >  Article  >  Development Tools  >  Detailed explanation of how PHP can make better use of PHPstorm’s automatic prompts

Detailed explanation of how PHP can make better use of PHPstorm’s automatic prompts

(*-*)浩
(*-*)浩Original
2019-11-26 11:37:383273browse

Detailed explanation of how PHP can make better use of PHPstorm’s automatic prompts

After writing Java for a while, I am particularly not used to the weakly typed method of PHP itself. I always feel uneasy when writing code, especially since PHP itself is a weakly typed language. , so when coding, there are often no code prompts.

A general example (Recommended Learning: PHPSTORM Detailed Explanation )

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

## The above example, in general, there is no existence What's the problem, but when writing the code

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

, there is no automatic prompt when calling attributes. So when the amount of data is large, you need to scroll up and copy or write it down. , reducing the coding speed, and sometimes I don’t know what to write, and I am afraid of making mistakes.

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 to determine whether the data type is It 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,

Judging from the above example, doing this and adding some error handling mechanisms 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 how PHP can make better use of PHPstorm’s automatic prompts. 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