PHP7擴充開發之實現簡單運算函數

來源:互聯網
上載者:User

標籤:comment   support   whether   hello   enable   php   

實現一個簡單的數學計算函數,傳入3個參數,計算出相應的結果。 如:  echo "90+20=".cal_culator(90,20,‘+‘); echo "90-20=".cal_culator(90,20,‘-‘); echo "90*20=". cal_culator(90,20,‘*‘); echo "90/20=". cal_culator(90,20,‘/‘); echo "90%20=". cal_culator(90,20,‘%‘);     1.建立一個新的擴充   ./ext_skel --extname=hello  2.vi config.m4   去掉以下3行行首的dnl          PHP_ARG_ENABLE(hello, whether to enable strive support,          Make sure that the comment is aligned:          [  --enable-hello           Enable strive support])              3,編寫代碼 1.vi hello.c2.#添加下面的代碼     PHP_FUNCTION(cal_culator)                {                    double num1 = 0.0;                    double num2 = 0.0;                    zend_string *type;                   //php7之前用此方式來解析參數                    #ifndef FAST_ZPP                     if (zend_parse_parameters(ZEND_NUM_ARGS(),TSRMLS_CC, "dd|S", &num1, &num2,&type) == FAILURE) {                        return;                    }                    #else                       ZEND_PARSE_PARAMETERS_START(2, 3)                          Z_PARAM_DOUBLE(num1)                          Z_PARAM_DOUBLE(num2)                          Z_PARAM_OPTIONAL                          Z_PARAM_STR(type)                        ZEND_PARSE_PARAMETERS_END();                   #endif                  switch(ZSTR_VAL(type)[0])                  {                        case ‘+‘:                             RETURN_DOUBLE(num1+num2);                             break;                        case ‘-‘:                             RETURN_DOUBLE(num1-num2);                             break;                        case ‘*‘:                             RETURN_DOUBLE(num1*num2);                             break;                        case ‘/‘:                             RETURN_DOUBLE(num1/num2);                             break;                        case ‘%‘:                             RETURN_LONG((int)num1%(int)num2);                             break;                   }         }           3.在數組中添加函數名:  const zend_function_entry  hello_functions[] = {          PHP_FE(cal_culator, NULL) //cal_culator function      }         參數解析說明:     //php7之前的參數解析方法             if (zend_parse_parameters(ZEND_NUM_ARGS(),TSRMLS_CC, "dd|S", &num1, &num2,&type) == FAILURE) {           return;          }                          ZEND_NUM_ARGS() :內建函數,固定格式,【告訴Zend引擎要取的參數的資訊】      TSRMLS_CC     :用來確保安全執行緒       "dd|S"      : 傳入參數類型【詳見下面參數類型表】可傳入2個double型別參數,或傳入2個double類型參與與一個string型別參數      &num1,&num2...  :傳入對應參數的儲存地址。              //php7中的參數解析方法                          ZEND_PARSE_PARAMETERS_START(2, 3)     //開始參數解析,最少傳2個參數,最多3個參數          Z_PARAM_DOUBLE(num1)       //將第一個參數轉換為double類型,傳入參數儲存地址          Z_PARAM_DOUBLE(num2)       //將第二個參數轉換為double類型, 傳入參數儲存地址          Z_PARAM_OPTIONAL         //表示|,意為後面的參數可以不傳          Z_PARAM_STR(type)        //第3個參數轉換為string類型, 傳入參數儲存地址        ZEND_PARSE_PARAMETERS_END();      //結束參數解析                     參數類型表:          d: double 類型          s: string 類型          l:long 類型           |:或的意思,表示後面的可傳可不傳                             傳回值說明:          RETURN_DOUBLE   :返回浮點型          RETURN_LONG     :返回整型               4.編譯安裝擴充,       phpize       ./configure --with-php-config=php_conf_dir       make && make install       vi php.ini         extension=strive.so              reload php-fpm    5.測試cal_culator函數是否可用:             php -r ‘echo cal_culator(10,40,‘+‘);‘

   源碼地址:https://github.com/yq918/php-ext-strive.git

   若實現方法有誤或有更好的實現方法,歡迎大家給予批評與指導。

 

PHP7擴充開發之實現簡單運算函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.