PHP Development Primer 1

Source: Internet
Author: User
Have always wanted to learn PHP extension development. Every time I see so many C code, I don't know what to do. Once also developed a Hello world with. But I still don't understand.

Recently, you need a method that generates a random string. Feel that native PHP generation is somewhat slow (mainly to try to learn how to write extensions). So I wrote an extension with Zehphir.

Assi it. The speed is slower than the native. Before also tested Zephir, this seemingly does not load PHP native function when also can, once called the native function, the speed will drop down again. So want to learn zephir, or think of him as a code encryption method.

So, consider using C. So how to develop it?

Baidu, Google and other search out a lot of tutorials. A lot of them say starters write a def file. This is the way I used it the first time I did it. But this time I didn't find the Def writing tutorial. So look for tutorials.

In the end, I found that writing extensions basically required adding definitions in several places, or adding functionality. Of course it's just a simple function. How to develop classes that have not yet been seen, have the information of friends can provide some.

First, Ext_skel generates extension code.

./ext_skel--extname=phpext

Then, go to the Phpext directory and you can see Php_phpext.h and phpext.c. Next, first open the Php_phpext.h and find the following code

Php_minfo_function (Phpext);

Add the function you want to add below, where the function name needs to customize an English string

Php_function (function name)

Next Open phpext.c, find the following code. Here is the PHP entry function, you need to tell PHP what functions you have.

Const Zend_function_entry ukey_functions[] = {}

Enter the name of the PHP function you just added

Php_fe (function name, NULL)

The following will enlarge the recruit. The ability to add functions. Add features at the end of the phpext.c

Php_function (function name) {//Here fill in function if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "L", &length) = = FAILURE)// Here is a parameter for shaping  . Length needs to be defined previously, the parameters obtained here are assigned to length{    return_null ();//Return NULL  }}


PHP built-in return function

#define RETVAL_RESOURCE (L) zval_resource (Return_value, L) #define Retval_bool (b) Zval_bool (Return_value, b) #define Retval_null () zval_null (return_value) #define Retval_long (L) Zval_long (Return_value, L) #define Retval_double (d) ZVAL_ DOUBLE (Return_value, D) #define Retval_string (S, duplicate) zval_string (Return_value, S, duplicate) #define Retval_ Stringl (S, l, duplicate) Zval_stringl (Return_value, S, l, duplicate) #define retval_empty_string () zval_empty_string ( Return_value) #define RETVAL_ZVAL (Zv, copy, Dtor) zval_zval (Return_value, Zv, copy, dtor) #define Retval_false Zval_bool ( Return_value, 0) #define Retval_true zval_bool (return_value, 1) #define RETURN_RESOURCE (l) {retval_resource (L); return; } #define Return_bool (b) {retval_bool (b); RETURN;} #define Return_null () {retval_null (); RETURN;} #define Return_long (L) {Retval_long (l); RETURN;} #define Return_double (d) {retval_double (d); RETURN;} #define RETURN_STRING (S, duplicate) {retval_string (S, duplicate); RETURN;} #define RETURN_STRINGL (s), L, duplicate) {Retval_stringl (S, l, duplicate); return;} #define Return_empty_string () {retval_empty_string (); RETURN;} #define RETURN_ZVAL (Zv, copy, dtor) {retval_zval (Zv, copy, dtor); RETURN;} #define RETURN_FALSE {retval_false; RETURN;} #define RETURN_TRUE {retval_true; RETURN;}

PHP receives and formats parameters

Bbooleanlinteger integer dfloating point float sstring string rresource resource Aarray Array Oobject instance object Oobject instance of a specified t Ype a specific type of object znon-specific zval any type ~zzval** type F denotes a function, method name, PHP5.1 in the seemingly wood has ....
  • 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.