PHP meta-language programming

Source: Internet
Author: User
Meta-language programming, are used in the most flexible place.

The most recent task is to design an extremely flexible test item, and the biggest difficulty is that several sets of test papers have different scoring rules.

If you want to follow the general method, you may write six sets of programs to finish, but there are a lot of the same parts, if the design is not good, it will lead to a lot of code redundancy, resulting in maintenance difficulties.

To this end, to elaborate a method, the use of meta-language programming, the different logic written to the configuration file, the formation of a unified rule, you can complete the task.

Gossip eval function

PHP is a flexible scripting language, since it is a scripting language, it will generally provide a corresponding meta-programming method, the oldest is the Eval function.

The early implementation of the Eval function in Lisp is widely accepted, and the Eval function takes a string as a parameter (which is, of course, a list in Lisp), and then executes the value in the string as code.

The Eval function actually invokes the parser intrinsic function by parsing the text, parsing it, processing the current symbol table, and then executing it.


   
    $string = 'Bierglas';$name = 'Binding-Lager';$str = 'Das ist mein $string, voll mit $name.';echo$str . "\n";eval ("\$str = \"$str\";");echo$str . "\n";

This PHP code is a good example of the Eval function, of course, there is an important feature in PHP, the single quotation mark is a pure string, and the double quotation marks the part, it contains a large number of escape information, such as: $ at the beginning of the variable will be formatted, and \ N is also only in double quotation marks is the line, otherwise only output \ n.

Here is the result of the execution:

$ Das$string$name.DasBierglasBinding-Lager.

Special calls to PHP functions

Say the Eval function, in fact, PHP has some convenient internal functions can be used.

call_user_func, you can call a PHP function based on the name in the string
call_user_func_array, the passed parameter can be an array, which avoids the problem of passing the indefinite parameter.


   
    /** * @Author: sxf * @Date:   2015-07-29 23:00:36 * @Last Modified by:   sxf * @Last Modified time: 2015-07-29 23:36:16 */functiona($b, $c)    {echo$b ."\n";        echo$c ."\n";    }    call_user_func_array('a', array("111", "222")); 

This article belongs to the West Wind getaway original, reproduced please indicate the source: West Wind World http://blog.csdn.net/xfxyy_sxfancy

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above is the introduction of PHP meta-language programming, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.