在php中,使用者函式的定義從function關鍵字開始。如下所示簡單示例:
function foo($var)
這是乙個非常簡單的函式,它所實現的功能是定義乙個函式,函式有乙個引數,函式的內容是在標準輸出端輸出傳遞給它的引數變數的值。
函式的一切從function開始。我們從function開始函式定義的探索之旅。
在 zend/zend_language_scanner.l中我們找到如下所示的**:
"function"
它所表示的含義是function將會生成t_function標記。在獲取這個標記後,我們開始語法分析。
在 zend/zend_language_parser.y檔案中找到函式的宣告過程標記如下:
關注點在 function is_reference t_string,表示function關鍵字,是否引用,函式名。function:
t_function
;is_reference:
/* empty */
| '&'
;unticked_function_declaration_statement:
function is_reference t_string
'(' parameter_list ')' ''
;
語法解析後,我們看到所執行編譯函式為zend_do_begin_function_declaration。在 zend/zend_complie.c檔案中找到其實現如下:
生成的中間**為 zend_declare_function ,根據這個中間**及運算元對應的op_type。 我們可以找到中間**的執行函式為 zend_declare_function_spec_handler。void zend_do_begin_function_declaration(znode *function_token, znode *function_name,
int is_method, int return_reference, znode *fn_flags_znode tsrmls_dc) /* else
}/* }}} */
在生成中間**時,可以看到已經統一了函式名全部為小寫,表示函式的名稱不是區分大小寫的。
為驗證這個實現,我們看一段**:
執行**,可以看到螢幕上輸出如下報錯資訊:function t()
function t()
fatal error: cannot redeclare t() (previously declared in ...)
表示對於php來說t和t是同乙個函式名。檢驗函式名是否重複,這個過程是在哪進行的呢? 下面將要介紹的函式宣告中間**的執行過程包含了這個檢查過程。
在 zend/zend_vm_execute.h 檔案中找到 zend_declare_function中間**對應的執行函式:zend_declare_function_spec_handler。 此函式只呼叫了函式do_bind_function。其呼叫**為:
do_bind_function(ex(opline), eg(function_table), 0);
在這個函式中將ex(opline)所指向的函式新增到eg(function_table)中,並判斷是否已經存在相同名字的函式,如果存在則報錯。 eg(function_table)用來存放執行過程中全部的函式資訊,相當於函式的登錄檔。 它的結構是乙個hashtable,所以在do_bind_function函式中新增新的函式使用的是hashtable的操作函式zend_hash_add。 Python 函式 函式的定義 過程和引數
一 函式 是邏輯結構化和過程化的一種程式設計方法 函式名重複是以最後乙個為次函式 優點 重用 保持一致性 易保護 可擴充套件性。定義 函式關鍵字 def test x 函式名及引數,內定義形參 the function deinitions 文件描述 非必要但是最好寫 x 1 塊區域 return ...
MySQL定義函式和儲存過程
預設為off 等價於 0 開啟binlog日誌記錄時,是否信任 建立函式 預設情況下,為了防止主機和從機中定義的函式不一致,不允許使用者自定義函式 如果希望自定義函式,必須將 log bin trust function creators on 1 show variables like log b...
自定義函式(def) 函式與過程
1.函式返回值 def say print itxds print say itxds none 函式沒有顯性返回時,預設返回none 2.變數作用域 區域性變數 def cal amount,rate finalamount amount rate return finalamount cal 1...