search
HomeBackend DevelopmentPHP TutorialSharing of specific improvement techniques for PHP function parameter passing methods_PHP Tutorial

Sharing of specific improvement techniques for PHP function parameter passing methods_PHP Tutorial

Jul 15, 2016 pm 01:29 PM
phptransferspecificWritefunctionshareparameterexistusSkilloperateImprovemethodofrepeat

When we were writing After many repeated operations, I decided to improve the traditional PHP function parameter passing method and use arrays as parameters. Please see the example below.

Let’s first look at a traditional custom function

<ol class="dp-xml">
<li class="alt"><span><span>/**  </span></span></li>
<li class=""><span>* @Purpose:     插入文本域  </span></li>
<li class="alt"><span>* @Method Name: addInput()  </span></li>
<li class=""><span>* @Parameter:    str $title        表单项标题  </span></li>
<li class="alt"><span>* @Parameter:    str $name        元素名称  </span></li>
<li class=""><span>* @Parameter:    str $value        默认值  </span></li>
<li class="alt"><span>* @Parameter:    str $type        类型,默认为text,可选password  </span></li>
<li class=""><span>* @Parameter:    str $maxlength        最长输入  </span></li>
<li class="alt"><span>* @Parameter:    str $readonly        只读  </span></li>
<li class=""><span>* @Parameter:    str $required        是否必填,默认为false,true为必填  </span></li>
<li class="alt"><span>* @Parameter:    str $check        表单验证function(js)名称  </span></li>
<li class=""><span>* @Parameter:    str $id            元素id,无特殊需要时省略  </span></li>
<li class="alt"><span>* @Parameter:    int $width        元素宽度,单位:象素  </span></li>
<li class=""><span>* @Parameter:    str $tip        元素提示信息  </span></li>
<li class="alt"><span>* @Return:        </span></li>
<li class=""><span>*/  </span></li>
<li class="alt">
<span>function addInput($title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,<br>$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>;  </span>
</li>
<li class="">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n";  </span>
</li>
<li class="alt">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>=""<br>.$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>=""<br>.$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width.<br>"px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> ";  </span>
</li>
<li class="">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n";  </span>
</li>
<li class="alt">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span>
</li>n"<span>;  </span><li class=""><span>} </span></li>
</ol>

This is the form class I wrote A function that inserts a text box.

The calling method of the PHP function parameter passing method is

<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,""); </span></span></li></ol>

At the beginning, only $title, $name, $value, $type, $maxlength, $readonly and other parameters were reserved. After a period of use, it was found that these basic parameters could not meet the needs. Text box JS verification is required, CSS styles need to be defined, prompt information needs to be added, etc...

After adding $required, $check, $id, $width, $tip and other parameters, I found that all previous calls to this function were found Everything needs to be modified, which adds a lot of workload.

The calling method of the PHP function parameter passing method becomes

<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,"","true",""<br>,"",100,"提示:编号为必填项,只能填写3位");  </span></span></li></ol>

If there are many places where this function is used, it will really take a long time to change each one.

The following is my improved function

<ol class="dp-xml">
<li class="alt"><span><span>function addInput($a)  </span></span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>    if(is_array($a))  </span></li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">title</font></span><span>        = $a['title'];  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">name</font></span><span>        = $a['name'];  </span>
</li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">value</font></span><span>        = $a['value'] ? $a['value'] : "";  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">type</font></span><span>        = $a['type'] ? $a['type'] : "text";  </span>
</li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>    = $a['maxlength'] ? $a['maxlength'] : "255";  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">readonly</font></span><span>    = $a['readonly'] ? $a['readonly'] : "";  </span>
</li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">required</font></span><span>    = $a['required'] ? $a['required'] : "false";  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">check</font></span><span>        = $a['check'];  </span>
</li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">id</font></span><span>        = $a['id'];  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">width</font></span><span>        = $a['width'];  </span>
</li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">tip</font></span><span>        = $a['tip'];  </span>
</li>
<li class=""><span>    }  </span></li>
<li class="alt">
<span>    $title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip  </span>
</li>
<li class="">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>;  </span>
</li>
<li class="alt">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n";  </span>
</li>
<li class="">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>="".$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>="".$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width."px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> ";  </span>
</li>
<li class="alt">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n";  </span>
</li>
<li class="">
<span>    $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span>
</li>n"<span>;  </span><li class="alt"><span>} </span></li>
</ol>

The calling method becomes >

<ol class="dp-xml">
<li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput(  </span></span></li>
<li class=""><span>    array(  </span></li>
<li class="alt"><span>        'title' = "编码",  </span></li>
<li class=""><span>        'name' = "field0",  </span></li>
<li class="alt"><span>        'maxlength' = 3,  </span></li>
<li class=""><span>        'required' = "true",  </span></li>
<li class="alt"><span>        'width' = 100,  </span></li>
<li class=""><span>        'tip' = "提示:编号为必填项,只能填写3位",  </span></li>
<li class="alt"><span>    )  </span></li>
<li class=""><span>);  </span></li>
</ol>
Passed by A comparison of the PHP function parameter passing methods before and after can be found:

Traditional functions change a lot when they need to be expanded. When used, they must be written in the order of parameters, which is easy to make mistakes.

When the improved function is expanded You can add new parameters at any time. You only need to add the corresponding array key value when calling. Each parameter is clear at a glance without considering the order. The readability of the code is enhanced.

However, there are still improvements in the PHP function parameter passing method. The disadvantage is that the amount of code increases, requiring programmers to write a lot more key values, and the judgment statements and ternary operation statements in the function may affect efficiency.


http://www.bkjia.com/PHPjc/446329.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446329.htmlTechArticleWhen we were writing, I decided to improve the traditional PHP function parameter passing method after going through many repeated operations, using Array as parameter, please see the example below. First look at a traditional automatic...
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
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

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 Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.