search
HomeBackend DevelopmentPHP ProblemWhat to do if the php method has too many parameters

What to do if the php method has too many parameters

Nov 25, 2021 am 09:44 AM
php methodparameter

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.

What to do if the php method has too many parameters

#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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software