## 示例
### 用PHP實現
```php
<?php
const TEMP_CONVERTER_TO_CELSIUS = 1;
const TEMP_CONVERTER_TO_FAHREINHEIT = 2;
//華氏轉攝氏
function fahrenheit_to_celsius($fahrenheit){
return 5/9 * ($fahrenheit - 32);
}
//攝氏轉華氏
function celsius_to_fahrenheit($celsius){
return 9/5 * $celsius + 32;
}
//雙向轉換
function temperature_converter($temp, $type = TEMP_CONVERTER_TO_CELSIUS){
switch ($type) {
case TEMP_CONVERTER_TO_CELSIUS:
return sprintf("華氏 %.2f 度, 攝氏 %.2f 度", $temp, fahrenheit_to_celsius($temp));
case TEMP_CONVERTER_TO_FAHREINHEIT:
return sprintf("攝氏 %.2f 度, 華氏 %.2f 度", $temp, celsius_to_fahrenheit($temp));
default:
trigger_error("轉換模式參數錯誤, 可接受的轉換模式值為 1 或 2", E_USER_WARNING);
break;
}
}
//華氏數組批量轉攝氏
function multiple_fahrenheit_to_celsius(array $temperatures){
foreach ($temperatures as $temp) {
$return[] = fahreinheit_to_celsius($temp);
}
return $return;
}
```
### 用擴展實現
1. 生成骨架.
`./ext_skel --extname=pib --proto=./pib_proto --no-help`
pib_proto:
```
double fahrenheit_to_celsius (double f)
double celsius_to_fahrenheit (double c)
string temperature_converter (double t, long mode)
array multiple_fahrenheit_to_celsius(array temperatures)
```
2. 編寫代碼 : 代碼在./code/1-5-2/pib
3. 編譯測試
**編譯:**
```shell
cd ext/pib
../../../php-7-1-8-install/bin/phpize
./configure --with-php-config=../../../php-7-1-8-install/bin/php-config
make
```
**運行時配置**
**擴展測試**
```php
<?php
echo celsius_to_fahrenheit(35);
echo fahrenheit_to_celsius(95);
echo temperature_converter(95,1);
print_r(multiple_fahrenheit_to_celsius(array(94,69,120)));
//查看擴展的反射信息,等于命令行中執行 php -dextension=pib.so --re pib
//如果不聲明參數的話,反射獲取不到參數信息
//print_r(ReflectionExtension::export('pib'));
```
### 擴展中的常量
```c
/* 常量的數據結構 */
typedef struct _zend_constant {
zval value;
zend_string *name;
int flags;
int module_number;
} zend_constant;
/* 注冊常量的宏: */
/* 注冊NULL常量 */
#define REGISTER_NULL_CONSTANT(name, flags) \
zend_register_null_constant((name), sizeof(name)-1, (flags), module_number)
/* 注冊bool常量 */
#define REGISTER_BOOL_CONSTANT(name, bval, flags) \
zend_register_bool_constant((name), sizeof(name)-1, (bval), (flags), module_number)
/* 注冊整形常量 */
#define REGISTER_LONG_CONSTANT(name, lval, flags) \
zend_register_long_constant((name), sizeof(name)-1, (lval), (flags), module_number)
/* 注冊浮點型常量 */
#define REGISTER_DOUBLE_CONSTANT(name, dval, flags) \
zend_register_double_constant((name), sizeof(name)-1, (dval), (flags), module_number)
/* 注冊字符串常量,str類型為char* */
#define REGISTER_STRING_CONSTANT(name, str, flags) \
zend_register_string_constant((name), sizeof(name)-1, (str), (flags), module_number)
/* 注冊字符串常量,截取指定長度,str類型為char* */
#define REGISTER_STRINGL_CONSTANT(name, str, len, flags) \
zend_register_stringl_constant((name), sizeof(name)-1, (str), (len), (flags), module_number)
```
#### TODO::擴展測試腳本的編寫
http://qa.php.net/write-test.php
### 練習
https://github.com/ThomasWeinert/php-extension-sample
https://github.com/Leon2012/php-ext
https://github.com/wzx19840423/php-extension/tree/master/src
### 跟蹤調試擴展
## 參考資料:
http://www.phpinternalsbook.com/index.html
https://github.com/pangudashu/php7-internal/
https://github.com/tvlooy/php-ext-dev-book