In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the php method too many parameters of the solution, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
The solution to too many parameters of php method: 1, objectify the parameters; 2, define a BookModel class; 3, transform the create method to require its parameters to be BookModel class.
The operating environment of this paper: Windows7 system, PHP7.1, Dell G3.
Optimization scheme with too many parameters for PHP method
When we write a PHP method, we usually have several parameters, like the following code:
Class Book {public function create ($name, $cateId, $author) {$params = ['name' = > $name,' cateId' = > $cateId, 'author' = > $author];}}
There's no problem.
However, as the business develops, the parameters may continue to increase. As in the example above, creating a book starts with only three parameters name/cateId/author, and slowly becomes something like this:
Class Book {public function create ($name, $cateId, $author, $year, $price, $publish, $country, $language) {$params = ['name' = > $name,' cateId' = > $cateId, 'author' = > $author,' year' = > $year, 'price' = > $price,' publish' = > $publish, 'country' = > $country,' language' = > $language ] }}
It works well! But it always seems not very elegant, when you call this method, God knows what the order of the parameters is!
How to optimize it? We can try to objectify the parameters. Take a look at the following code:
Class BookModel {protected $name; protected $cateId; protected $author; protected $year; protected $price; protected $publish; protected $country; protected $language; public function getName () {return $this- > name;} public function setName ($name) {$this- > name = $name;} public function getCateId () {return $this- > cateId;} public function setCateId ($cateId) {$this- > cateId = $cateId } public function getAuthor () {return $this- > author;} public function setAuthor ($author) {$this- > author = $author;} public function getYear () {return $this- > year;} public function setYear ($year) {$this- > year = $year;} public function getPrice () {return $this- > price } public function setPrice ($price) {$this- > price = $price;} public function getPublish () {return $this- > publish;} public function setPublish ($publish) {$this- > publish = $publish;} public function getCountry () {return $this- > country;} public function getLanguage () {return $this- > language } public function setLanguage ($language) {$this- > language = $language;}}
A BookModel class is defined above, which contains some properties. Then we modify the create method to require its argument to be the BookModel class. Because the data structure of BookModel is clear, it is very convenient to use. After adjusting the create method:
Class Book {public function create (BookModel $bookModel) {$params = ['name' = > $bookModel- > getName (),' cateId' = > $bookModel- > getCateId (), 'author' = > $bookModel- > getAuthor (),' year' = > $bookModel- > getYear (), 'price' = > $bookModel- > getPrice (),' publish' = > $bookModel- > getPublish () 'country' = > $bookModel- > getCountry (),' language' = > $bookModel- > getLanguage (),] }}
Look, the advantages of object-oriented programming are highlighted here!
Thank you for reading this article carefully. I hope the article "the solution to too many parameters of php method" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.