Get the App
SLTechnology News&Howtos  ›  Development  › 

The solution of too many parameters in php method

Shulou Source: shulou.com Published: 2022-06-02 11:09:09 10月06日 Update

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!

Tags: Parameters methods articles objects facets code transformation elegance three continuous business value advantages examples interests methods only at the same time attributes data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Microsoft vpn NVidia