Design Mode-how to solve the problem of too long function parameter list in php

Source: Internet
Author: User
To write a function that can splice SQL statements flexibly, select, from, where, groupby, orderby, and limit are passed as parameters. However, if each parameter is passed as an independent parameter, the order of parameters will be limited when calling the function, and several simple SQL statements may be written... to write a function that can splice SQL statements flexibly, select, from, where, group by, order by, and limit are passed as parameters. However, if each parameter is passed as an independent parameter, the order of parameters is limited when a function is called. A simple SQL statement may also write several useless null parameters. If you want to construct an object to pass all the parameters, the parameter list is too short and there is no restriction on the order. However, it is quite troublesome to construct an object before each function call, it seems unnatural to use. Is there any good solution?

Reply content:

To write a function that can splice SQL statements flexibly, select, from, where, group by, order by, and limit are passed as parameters. However, if each parameter is passed as an independent parameter, the order of parameters is limited when a function is called. A simple SQL statement may also write several useless null parameters. If you want to construct an object to pass all the parameters, the parameter list is too short and there is no restriction on the order. However, it is quite troublesome to construct an object before each function call, it seems unnatural to use. Is there any good solution?

Function concatenationReadabilityPoor, that is,No code expression.

To the landlord a DSL of the portal, very can not correspond to the Chinese version of the http://en.wikipedia.org/wiki/Domain-s...

If you do not like the object Syntax:

query().from("user")    .where(      (user.firstName.like("Bob")).or(user.firstName.like("Ann")))    .orderBy(user.firstName.asc())    .list(user);

Object-oriented Syntax:

InsertSQL sql = new InsertSQL();sql.insertInto("students")   .value("id", new Integer(id))   .value("name", name)   .value("gender", gender);

-----------------------------------
Finally, I will give you a dsl I used before.

/*** Configure routing Rules */RouteBuilder builder = new RouteBuilder () {public void configure () {from ("/user "). filter (header ("language "). isdue to ("zh ")). to ("/zh/user"); from ("/user/edit/3 "). choice (). when (header ("foo "). isvice to ("bar ")). to ("queue: d "). when (header ("foo "). isrelative to ("cheese ")). to ("queue: e "). otherwise (). to ("queue: f ");}};

The expressive code is:What to do, not how to do.

Sometimes, I will make an encapsulation similar to this and provide it to the landlord for reference ......

// Pseudo-code class SQL {protected $ fields, $ table, $ where; function _ construct ($ table) {$ this-> table = $ table ;} function select ($ fields) {$ this-> fields = $ fields; return $ this;} function where ($ where) {$ this-> where = $ where; return $ this;} function _ toString () {return "select {$ this-> fields} from {$ this-> table} where {$ this-> where}";} static function from ($ table) {return new self ($ table) ;}// use echo SQL: from ('table')-> select ('*') -> where ('1 = 1'); // output: select * from table where 1 = 1;

Of course, the encapsulation actually used will be more complicated than this ......

I don't know how many people have done this kind of "flexible" SQL fight. I think they will eventually find that SQL is too flexible and the language is too rigid.

A feasible practice is to centralize SQL statements. In fact, there are not many variables in the SQL statement that goes to the database each time. You can write the SQL statement in advance, assign an id, simply wrap it, and provide parameters during the call.

There are at least two advantages:
1. Make sure that the SQL statement has been tested and the indexes used are used.
2. parameterized query can prevent SQL injection attacks

I personally prefer to pass these variable parameters into an array ($ options... then, the function determines whether this parameter is passed in... I don't know if I did not answer this question...

  1. For SQL, it is recommended to directly spell SQL or find a framework. For example, the Yii framework supports the following syntax: (is it much simpler ?)
$rows = (new \yii\db\Query())    ->select(['id', 'email'])    ->from('user')    ->where(['last_name' => 'Smith'])    ->limit(10)    ->all();
  1. For the question title itself -- "How to Solve the Problem of too long function parameter list in php" -- PHP has a powerful array that can be used to transmit parameters (named parameters in disguise ):
function foo($params){    $params = array_merge(array(            'select' => 'default selector',            'where' => 'default where',            // ...       ), $params);    // do something with $params...}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.