php7擴充自動載入類.

來源:互聯網
上載者:User
使用php擴充來開發架構似乎越來越來成來主流了,如phalcon,鳥哥的yaf。這些架構都以高效能著稱,相對純php使用的架構而,php擴充實現的架構更快,不會帶來額外的系統消耗。在php啟動時便已載入了架構,並且常駐記憶體。 幾乎所有架構都帶自動載入類,以便更好的管理代碼。php實現方法這裡就不多介紹了,讀者可以自行百度。這裡將介紹如何在php擴充中實現。 擴充中的實現原理與php實現基本原理一致,都是基於 spl_autoload_register 函數實現。
ZEND_METHOD(lxx_request,run) {    zval arg;//參數    zval * pthis = getThis();//當前類    array_init(&arg);//初始化資料,    Z_ADDREF_P(pthis);    add_next_index_zval(&arg, pthis);    add_next_index_string(&arg, "load");//當前類的中load的方法,表態方法(ZEND_ACC_STATIC)    /*zend_call_method_with_1_params arg為數組參婁 php 代碼spl_autoload_register(['lxx\request','load']);*/    zend_call_method_with_1_params(NULL, NULL, NULL, "spl_autoload_register", NULL, &arg);   zval_ptr_dtor(&arg);}/*字元替換*/static void str_replace(char *str,char o,char n) {    while(*str != '\0') {        if (*str == o) {            *str = n;        }        str++;    }} ZEND_METHOD(lxx_request,load) {    zend_string *cl;       /*php7中新增的資料類型 全用大寫S接收,小寫s則char指標*/    long cl_len = 0;    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &cl) == FAILURE) {        RETURN_FALSE;    }    zend_file_handle zfd;    zend_op_array *op_array;    smart_str str = {0};    smart_str_appendl(&str,cl->val,cl->len);    smart_str_appendl(&str,".php",sizeof(".php")-1);    smart_str_0(&str);    str_replace(str.s->val,'\\','/');    zfd.type = ZEND_HANDLE_FILENAME;    zfd.filename = str.s->val;    zfd.free_filename = 0;    zfd.opened_path = NULL;    zfd.handle.fp = NULL;    op_array = zend_compile_file(&zfd,ZEND_INCLUDE);     if (zfd.opened_path) {        zend_hash_add_empty_element(&EG(included_files),zfd.opened_path);    }    zend_destroy_file_handle(&zfd);    if(op_array) {        zend_execute(op_array,NULL);        //zend_exception_restore();        destroy_op_array(op_array);        efree(op_array);    }    //zend_printf("
%s 有沒有啊?帥不帥啊?",str.s->val); zend_string_release(cl); smart_str_free(&str);}

註冊當前類

ZEND_MODULE_STARTUP_D(lxx_request) {    zend_class_entry ce;    memset(&ce,0,sizeof(zend_class_entry));    INIT_CLASS_ENTRY(ce,"Lxx\\request",lxx_request_functions);    lxx_request_ce = zend_register_internal_class_ex(&ce,NULL);    //lxx_request_ce->ce_flags = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;}

測試

$cl = new Lxx\request();$cl->run();$ab = new abc\ab();echo $ab->test("lxx");

在abc目錄下建立ab.php

namespace abc;class ab {    //php7新特性寫法,    public function test(string $name) :string {         return "Hi : ".$name;    }}

輸出

Hi : lxx


源文來自:www.lxxbl.com

  • 聯繫我們

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