<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 12.5 PHP語言中的超級全局變量(Superglobals) # 12.5 PHP語言中的超級全局變量(Superglobals) 在PHP中有一種“特殊”的全局變量,通常我們把它們稱作超級全局變量,常見的比如`$_GET`、`$_POST`、`$_FILE`等等。 他們會在編譯之前就聲明,所以在普通的腳本中,可能無法定義其它的超級全局變量。在擴展中,最好的使用超級全局變量的是session擴展,它使用`$_SESSION`來 在`session_start()`和`session_write_close()`之間存儲信息。那么是怎樣定義`$_SESSION`這個超級全局變量的呢?我們來看下session擴展的`MINIT`函數實現: ``` PHP_MINIT_FUNCTION(session) { zend_register_auto_global("_SESSION", sizeof("_SESSION") - 1, NULL TSRMLS_CC); return SUCCESS; } ``` 注意這里的第二個參數,`sizeof("_SESSION") - 1`,一定要排除標識字符串結束的\\0符。 我們一起來看下`zend_register_auto_global()`這個函數在ZE2中的原型: ``` int zend_register_auto_global(char *name, uint name_len, zend_auto_global_callback auto_global_callback TSRMLS_DC) ``` 在ZE1中,是沒有`auto_global_callback`這個參數的。為了和PHP4兼容,我們仍需要像下面這樣聲明一個超級全局變量: ``` PHP_MINIT_FUNCTION(sample4) { zend_register_auto_global("_SAMPLE4", sizeof("_SAMPLE4") - 1 #ifdef ZEND_ENGINE_2 , NULL #endif TSRMLS_CC); return SUCCESS; } ``` ### 全局變量的回調 在ZE2中,`zend_register_auto_global()`函數的`auto_global_callback`參數接受一個自定義函數。在實踐中,這樣的做法可以用來避免復雜的初始化,我們來 看下面這一段代碼: ``` zend_bool php_sample4_autoglobal_callback(char *name, uint name_len TSRMLS_DC) { zval *sample4_val; int i; MAKE_STD_ZVAL(sample4_val); array_init(sample4_val); for(i = 0; i < 10000; i++) { add_next_index_long(sample4_val, i); } ZEND_SET_SYMBOL(&EG(symbol_table), "_SAMPLE4", sample4_val); return 0; } PHP_MINIT_FUNCTION(sample4) { zend_register_auto_global("_SAMPLE4", sizeof("_SAMPLE4") - 1 #ifdef ZEND_ENGINE_2 , php_sample4_autoglobal_callback #endif TSRMLS_CC); return SUCCESS; } ``` 不幸的是,這樣的設計打破了PHP4和ZE1的規則,它們不支持這樣的回調。所以,為了兼容它們,我們要在每個腳本開始的時候去調用我們的回調函數(RINIT): ``` PHP_RINIT_FUNCTION(sample4) { #ifdef ZEND_ENGINE_2 php_sample4_autoglobal_callback("_SAMPLE4", sizeof("_SAMPLE4") - 1, TSRMLS_CC); #endif return SUCCESS; } ``` ## links - [目錄](preface.md) - 12.4 [PHP擴展中的全局變量](12.4.html) - 12.6 [小結](12.6.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看