Home > Article > Backend Development > What to do if the php method has too many parameters
The solution to the problem of too many parameters in the php method: 1. Objectify the parameters; 2. Define a BookModel class; 3. Modify the create method and require its parameters to be the BookModel class.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.
What should I do if the php method has too many parameters?
Optimization plan for excessive PHP method parameters
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 ]; } }
No problem.
However, as the business develops, the parameters may continue to increase. Just like the example above, when creating a book, there are only three parameters: name/cateId/author. Over time, it may become 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 inelegant. When you call this method, only the devil knows what the order of the parameters is!
How to optimize? We can try to objectify the parameters. Please 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; } }
The above defines a BookModel class, which contains some properties. Then we modify the create method and require its parameter to be the BookModel class. Since 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!
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if the php method has too many parameters. For more information, please follow other related articles on the PHP Chinese website!