<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 前言 上一篇文章,我們使用的是函數方式調用lib庫。這篇文章我們將使用對象的方式調用lib庫。調用代碼如下: ``` <?php $hello = new hello(); $result = $hello->get(); var_dump($result); ?> ``` 我們將在擴展中實現hello類。hello類中將依賴lib庫。 ## 代碼 ### 基礎代碼 這個擴展,我們將在say擴展上增加相關代碼。say擴展相關代碼大家請看這篇博文。PHP7擴展開發之hello word 文中已經詳細介紹了如何創建一個擴展和提供了源碼下載。 ### 代碼實現 #### 建立lib庫 增加hello.h文件。代碼如下: ```c #ifndef TEST_HEADER_FILE #define TEST_HEADER_FILE #include <stdlib.h> #include <string.h> char * show_site(); #endif ``` 增加hello.c文件。代碼如下: ```c #include "hello.h" char * show_site() { char *site = malloc(15 * sizeof(char)); strcpy(site, "www.bo56.com"); return site; } ``` 然后使用以下命令生成lib庫(動態庫)文件: ``` $ gcc -g -O0 -fPIC -shared -o hello.so ./hello.c ``` 這樣在當前目錄下就會生成一個`hello.so`的動態庫文件。不同操作系統動態庫的擴展名可能不一樣。如 windows下是dll,mac下是 dylib,linux下是so。 然后把hello.so拷貝到`/usr/local/lib/`目錄下,命名為hello.so,把hello.h拷貝到`/usr/local/include/`目錄下。 #### 修改config.m4文件 增加擴展對動態庫的依賴。主要增加以下幾行代碼: ```c PHP_ADD_LIBRARY_WITH_PATH(hello, /usr/local/lib/, SAY_SHARED_LIBADD) PHP_SUBST(SAY_SHARED_LIBADD) ``` #### 編寫擴展代碼 修改say.c文件,增加hello.h的引用。 ```c #include "php_say.h" #include <stdio.h> //下面這行是增加的 #include "hello.h" ``` 聲明一個指針變量,用于存儲類的地址。 在`static int le_say;`行下面增加如下代碼: ``` zend_class_entry *hello_ce; ``` 聲明一個變量,用于存儲類的處理函數。增加如下代碼: ``` zend_object_handlers hello_object_handlers; ``` 定義一個結構體類型,用于存儲類的一些信息。如lib庫信息。 ```c typedef struct _hello_object { char * hello_ptr; zend_object std; } hello_object; ``` 注意,zend_object std一定要放在最后一行。 定義一個函數,用于從標準的zend_object轉換獲取hello_object地址。 ```c static inline hello_object *hello_fetch_object(zend_object *obj) /* {{{ */ { return (hello_object *)((char*)(obj) - XtOffsetOf(hello_object, std)); } ``` 定義一個函數,用戶創建類對象。代碼如下: ```c static zend_object * hello_object_create(zend_class_entry *type TSRMLS_DC) { hello_object *obj = (hello_object *)ecalloc(1, sizeof(hello_object) + zend_object_properties_size(type)); zend_object_std_init(&obj->std, type); object_properties_init(&obj->std, type); obj->std.handlers = &hello_object_handlers; return &obj->std; } ``` 定義函數,用戶銷毀類對象。代碼如下: ```c void hello_object_free_storage(zend_object *object) { hello_object *intern = hello_fetch_object(object); zend_object_std_dtor(&intern->std); } ``` 聲明類都有哪些方法。增加如下代碼: ```c PHP_METHOD(hello, __construct); PHP_METHOD(hello, __destruct); PHP_METHOD(hello, get); const zend_function_entry hello_methods[] = { PHP_ME(hello, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(hello, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) PHP_ME(hello, get, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} /* Must be the last line in hello_methods[] */ }; ``` 把上面聲明和定義的函數結合起來。 在`PHP_MINIT_FUNCTION`方法中增加如下代碼: ```c zend_class_entry ce; INIT_CLASS_ENTRY(ce, "hello", hello_methods); hello_ce = zend_register_internal_class(&ce); hello_ce->create_object = hello_object_create; memcpy(&hello_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); hello_object_handlers.clone_obj = NULL; hello_object_handlers.offset = XtOffsetOf(hello_object, std); hello_object_handlers.free_obj = hello_object_free_storage; ``` 實現在hello_methods中指定的類的方法: ```c PHP_METHOD(hello, __construct) { zval *self = getThis(); hello_object *obj = hello_fetch_object(Z_OBJ_P((self))); obj->hello_ptr = show_site(); RETURN_TRUE; } PHP_METHOD(hello, __destruct) { zval *self = getThis(); hello_object *obj = hello_fetch_object(Z_OBJ_P((self))); free(obj->hello_ptr); RETURN_TRUE; } PHP_METHOD(hello, get) { zval *self = getThis(); hello_object *obj = hello_fetch_object(Z_OBJ_P((self))); RETURN_STRING(obj->hello_ptr); } ``` php調用結果 ``` $php ./test.php string(12) "www.bo56.com" ``` ### 代碼解讀 類的創建,之前有一篇文章專門講述過流程。調用lib創建對象和普通創建對象不同之處: * 需要一個結構體來保存對象與lib庫資源之間的關系。如上面的hello_object。 * 需要定義一些方法。如創建對象,消耗對象,對象轉換的方法。 * 需要在PHP_MINIT_FUNCTION中把定義的方法和類綁定起來。
                  <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>

                              哎呀哎呀视频在线观看