php的c擴充

來源:互聯網
上載者:User

在php中最核心的一個資料結構就是這個:

typedef union _zvalue_value {
 long lval;   /* long value */
 double dval;  /* double value */
 struct {
 char *val;
 int len;
 } str;
 HashTable *ht;  /* hash table value */
 zend_object_value obj;
} zvalue_value;

struct _zval_struct {
 /* Variable information */
 zvalue_value value; /* value */
 zend_uint refcount;
 zend_uchar type; /* active type */
 zend_uchar is_ref;
};

這個資料結構也就是php中的每個資料類型在下層c語言中的表示。可以看到_zval_struct 結構體第一個欄位是一個聯合體,他來儲存實際的值,這些值可以為long,double,字串,hashtable(也就是php中的數組)和對象。而第2個參數是個引用計數,第三個參數是當前的類型。

也就是說php中的每個類型實際都是一個 struct _zval_struct類型。

1 首先進入php的源碼目錄下的ext檔案夾,然後運行下面的命令,這樣將會產生一個my_module的檔案夾。:

./ext_skel --extname=my_module

2 然後在my_module.h裡面聲明你的php函數名:

PHP_FUNCTION(my_function);

2 在my_module.c檔案的my_module_functions(這裡的module就是你所建立的擴充模組名字)加入你所要寫的php方法名:

PHP_FE(my_function, NULL)

3 接下來就實現PHP_FUNCTION(my_function)。

首先 參數的解析,當傳遞進來的php的類型和c的類型之間的轉換:

這裡要用到的函數是:

int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);

其中的php類型和c類型的對應如下:

引用

l - long

d - double

s - string (with possible null bytes) and its length

b - boolean

r - resource, stored in zval*

a - array, stored in zval*

o - object (of any class), stored in zval*

O - object (of class specified by class entry), stored in zval*

z - the actual zval*

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.