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";
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();
In this place, PHPstorm will automatically prompt when calling the $item attribute based on this judgment, which is very convenient.
Thinking
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!