您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++開發(fā)PHP7中怎么定義常量”,在日常操作中,相信很多人在C++開發(fā)PHP7中怎么定義常量問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++開發(fā)PHP7中怎么定義常量”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
咱們定義如下幾個(gè)常量:(不要太糾結(jié)實(shí)際意義,哈哈,本身我們的這個(gè)擴(kuò)展都是臆想出來的)
HELLO_ZAPI_VERSION (int)
HELLO_ZAPI_NAME (string)
HELLO_DEBUG_MODE (bool)
HELLO_ZAPI_PI (double)
<h5 id="h5_0" box-sizing:inherit;background-color:#ffffff;-webkit-tap-highlight-color:transparent;"=""> zendAPI 常量描述類簡(jiǎn)單介紹
在 zendAPI 里面我們使用 zapi::lang::Constant 來描述一個(gè)常量的元信息,使用起來很簡(jiǎn)單,他的構(gòu)造函數(shù)接受兩個(gè)參數(shù),第一個(gè)參數(shù)是常量名稱,第二個(gè)參數(shù)是常量的值。例如我們使用下面代碼去定義一個(gè) ROOT_DIR 常量, 常量值是 /srv/www。
using zapi::lang::Constant; Constant dirConst("ROOT_DIR", "/srv/www");
zapi::lang::Constant API 手冊(cè)參考
現(xiàn)在大家學(xué)習(xí)完背景知識(shí),讓我們擼起袖子寫長(zhǎng)沙網(wǎng)站建設(shè)代碼吧。
C++ Code
#include "zapi/ZendApi.h" ?using zapi::lang::Constant; extern "C" { ZAPI_DECL_EXPORT void *get_module() { static zapi::lang::Extension hellozapi("hellozapi", "1.0"); ?Constant hellozapiVersionConst("HELLO_ZAPI_VERSION", 0x010002); Constant hellozapiNameConst("HELLO_ZAPI_NAME", "Hello zendAPI!"); Constant helloDebugModeConst("HELLO_DEBUG_MODE", true); Constant helloPiConst("HELLO_ZAPI_PI", 3.14); hellozapi.registerConstant(std::move(hellozapiVersionConst)); hellozapi.registerConstant(std::move(hellozapiNameConst)); hellozapi.registerConstant(std::move(helloDebugModeConst)); hellozapi.registerConstant(std::move(helloPiConst)); return hellozapi; } }
如果您對(duì) std::move 感到陌生,您可以閱讀 cpp reference 手冊(cè)
std::move 用戶手冊(cè)
怎么樣,就這么幾行,咱們的預(yù)定義常量就算定義好了,現(xiàn)在當(dāng)執(zhí)行我們 PHP 腳本的時(shí)候就可以直接使用了。
PHP Code
if (defined("HELLO_ZAPI_VERSION")) { echo HELLO_ZAPI_VERSION; } echo "\n"; if (defined("HELLO_ZAPI_NAME")) { echo HELLO_ZAPI_NAME; } echo "\n"; if (defined("HELLO_DEBUG_MODE")) { if (HELLO_DEBUG_MODE) { echo "true"; } else { echo "false"; } } echo "\n"; if (defined("HELLO_ZAPI_PI")) { echo HELLO_ZAPI_PI; } // you will get // ?65538 // Hello zendAPI! // true // 3.14
到此,關(guān)于“C++開發(fā)PHP7中怎么定義常量”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。