PHP7 Extension Auto-load class.

Source: Internet
Author: User
Tags php framework
Using PHP extensions to develop the framework seems to be becoming more and more mainstream, such as Phalcon, Brother Bird's Yaf. These frameworks are known for their high performance, and the PHP extension implements the framework faster than the pure PHP framework, and does not result in additional system consumption. When PHP is started, the framework is loaded and the memory is resident. Almost all frameworks have auto-load classes to better manage code. PHP Implementation method Here is not much introduced, the reader can own Baidu. Here is how to implement it in PHP extensions. The implementation principle in the extension is consistent with the basic principle of PHP implementation, which is based on the Spl_autoload_register function.
Zend_method (lxx_request,run) {zval arg;//parameter zval * pthis = Getthis ();//Current Class Array_init (&ARG);//Initialize data, Z_ad    dref_p (pthis);    Add_next_index_zval (&arg, pthis); Add_next_index_string (&arg, "load");//method of load in the current class, gesture method (zend_acc_static)/*zend_call_method_with_1_params Arg is an array of parameter bellow PHP code spl_autoload_register ([' lxx\request ', ' load '); */zend_call_method_with_1_params (NULL, NULL, NULL, "   Spl_autoload_register ", NULL, &arg); Zval_ptr_dtor (&arg);} /* Character replacement */static void Str_replace (char *str,char O,char N) {while (*str! = ')} {if (*str = = O) {*st        R = N;    } str++;       }} zend_method (Lxx_request,load) {zend_string *cl;    The new data type in/*PHP7 is received in uppercase S, lowercase s is char pointer */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 is there a yes? Handsome, not handsome? ", Str.s->val); Zend_string_release (CL); Smart_str_free (&STR);}

Registering the current class

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

Test

$CL = new Lxx\request (), $cl->run (), $ab = new Abc\ab (), Echo $ab->test ("Lxx");

Establish ab.php in the ABC directory

Namespace Abc;class ab {    //PHP7 new character notation, public    function test (string $name): string {         return ' Hi: '. $name;    }}

Output

Hi:lxx


SOURCE from: www.lxxbl.com

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.