Php implements strongly typed function return values

Source: Internet
Author: User
During the development process, the type of the return value of the function should be fixed, but PHP is a weak language, so PHP does not have such syntax verification, because of this, this has caused many pitfalls. During the development process, the type of the function return value should be fixed, but PHP is a weak language,

Therefore, PHP does not have such syntax verification, which causes many pitfalls.

For example, the following code:

 

The getArticles function returns different types of values based on different conditions, including bool, int, and array. Normally, such functions want to return an array and then take the array for some other operations,

However, because the types of function return values are not fixed, various unexpected pitfalls may occur during calling,

Therefore, I thought that since it cannot be standardized, it should be enforced directly.

The function/method return value can be of a forced type, as shown in the figure below.

Four types of mandatory restrictions are supported: int, array, bool, and object. if the returned value does not match the type in the function declaration, a warning error is thrown.

It's too embarrassing. it can only be regarded as an exception and cannot be regarded as an error, so we should use warning.

PHP itself does not support the syntax such as int function. to support the syntax, you must first set the syntax Parser. for the syntax parser, you can go here >>> view

For details, I will not talk about it here,

First modify the syntax to scan the Zend/zend_language_scanner.l file

Add the following code:

 
  ”int” {return T_FUNCTION_RETURN_INT;}
  
   ”bool” {return T_FUNCTION_RETURN_OBJECT;}
   
    ”object” {return T_FUNCTION_RETURN_OBJECT;}
    
     ”resource” {return T_FUNCTION_RETURN_RESOURCE;}
    
   
  
 

The scanner returns the corresponding T_FUNCTION _ * when scanning keywords int, bool, object, resource, and array. this is a token,

Tokens perform different processing based on different tokens. token must be defined in the Zend/zend_language_parser.y file first.

Add the following code:

.......... % Token T_FUNCTION_RETURN_INT % token T_FUNCTION_RETURN_BOOL % token T_FUNCTION_RETURN_STRING % token T_FUNCTION_RETURN_OBJECT % token handle and then add token processing logic: 1 function: T_FUNCTION {$. u. opline_num = CG (zend_lineno); $. u. EA. var = 0 ;}| T_FUNCTION_RETURN_INT T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_LONG;} | T_FUNCTION_RETURN_BOOL T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_BOOL;} | T_FUNCTION_RETURN_STRING T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_STRING ;}| T_FUNCTION_RETURN_OBJECT T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_OBJECT;} | T_FUNCTION_RETURN_RESOURCE T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_RESOURCE;} | T_ARRAY T_FUNCTION {$ $. u. opline_num = CG (zend_lineno); $. u. EA. var = IS_ARRAY ;}

$. U. EA. var stores the function return type, and finally matches it with the return value type,

In this way, the syntax interpreter can process our new php syntax.

This is not enough. you need to modify the processing logic defined in the function declaration.

Zend/zend_compile.c: zend_do_begin_function_declaration ...... Zend_op_array op_array; char * name = function_name-> u. constant. value. str. val; int name_len = function_name-> u. constant. value. str. len; int function_type = function_token-> u. EA. var; // Save the function type, added in the syntax interpreter: $. u. EA. var = IS_LONG; int function_begin_line = function_token-> u. opline_num ;...... Op_array.function_name = name; op_array.fn_type = function_type; // Save the type to op_array. op_array.return_reference = return_reference; required | = fn_flags; required = 0 ;..........

PHP parses the PHP syntax to generate the corresponding opcode, saves the required environment and parameter information to the execute_data global variable, and finally executes the opcode one by one through the execute function,

Therefore, you need to save the function type to opcode: op_array.fn_type = function_type;

Op_array does not have fn_type. to modify the op_array structure, add zend_uint fn_type;

(For opcode, you can imagine switching from c to assembly. I also have related articles in my blog. For more information, see)

Finally, modify the opcode destroy function. The return function generates token T_RETURN, and the T_RETURN function calls different calback functions based on the returned type:

ZEND_RETURN_SPEC_CONST_HANDLERZEND_RETURN_SPEC_TMP_HANDLERZEND_RETURN_SPEC_VAR_HANDLER

It has three callback. if the returned value is a const type data, ZEND_RETURN_SPEC_CONST_HANDLER
The returned value is temporary data, for example, return 1, then ZEND_RETURN_SPEC_TMP_HANDLER
The return value is a variable, for example, return $ a, then ZEND_RETURN_SPEC_VAR_HANDLER

Therefore, the processing logic must be added to the three callback functions:

Add the following code before the callback function return:

if((EG(active_op_array)->fn_type > 0) && Z_TYPE_P(retval_ptr) != EG(active_op_array)->fn_type){php_error_docref0(NULL TSRMLS_DC,E_WARNING, “function name %s return a wrong type.”, EG(active_op_array)->function_name );}

Fn_type is used to compare with the type of the returned value. If no match is found, this warning will be thrown.

I have installed a patch. Currently, only php5.3 is supported. if you need it, you can play it.

I don't know why the official website does not support this syntax. I think it is quite necessary.

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.