Explain how PHP can better utilize the automatic prompts of PHPstorm, and explain how phpstorm
Description
After writing java for a period of time, I am not used to the weak type of PHP. I always feel uneasy when writing code, especially when PHP is a weak type language, therefore, when coding, there are often no code prompts.
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 ('zhang san ', 'male', 18), new Data ('Li si', 'male', 14), new Data ('wang 5', 'male', 17 ), new Data ('Aunt, female, 23),];} private function eachData ($ data) {foreach ($ data as $ item) {echo $ item-> name. '=> '. $ item-> gender. '=> '. $ item-> age. "\ n" ;}}( new Test)-> run ();
In the above example, there is actually no problem, but
cho $item->name.'=>'.$item->sex.'=>'.$item->age."\n";
During this code, there is no automatic prompt when calling the attribute. When there is a large amount of data, you need to flip it up and copy or write it to reduce the encoding speed, and sometimes I am still confused, for fear of wrong writing.
The following is a complete example of using annotations and 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 ('zhang san ', 'male', 18), new Data ('Li si', 'male', 14), new Data ('wang 5', 'male', 17 ), new Data ('Aunt ', 'female', 23),];}/*** traverse output data * @ 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 ();
Here we mainly add an if judgment to determine whether the Data type is a specific instance of Data;
PHPstorm will automatically prompt you when calling the $ item Attribute Based on this judgment, which is very convenient.
Thoughts
Some of the ideas we get here are that we can better consider the rigor of programming. From the above examples, we have done this and added some error handling mechanisms, it can better ensure the security and integrity of data, not just the convenience of the editor prompts.
It is also very convenient to perform code check and tracking later, and the business logic is clearer.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.