Write the first PHP extension, implementing the number of computed arrays

Source: Internet
Author: User
Tags php source code
Requirements: Write the first PHP extension, which contains a function called Maxwelldu, Maxwelldu can calculate the length of the array (same as Count)

Requirements: Learn about C + + programming, familiar with PHP programming

System: CentOS6.5

Environment: LNMP (yum mode installation)

Stepping out the first step to start writing PHP extension, will use a tool, and this tool in the PHP source, so we download a PHP source code, http://php.net/downloads.php

CD ~mkdir softwarecd softwarewget http://cn2.php.net/distributions/php-5.6.11.tar.gztar zxvf PHP-5.6.11.TAR.GZCD Php-5.6.11/ext

#创建扩展项目, after the creation is completed, there will be one more SayHello folder in the Ext directory, which is our extension project.

./ext_skel--EXTNAME=MAXWELLDUCD Maxwellduvim CONFIG.M4

#打开允许, remove php_arg_enable before the line of DNL and [--enable-maxwelldu] this line before the DNL

Php_arg_enable (Maxwelldu, whether to ENABLE Maxwelldu support,dnl Do sure that the comment is aligned:[  --enable-max Welldu           Enable Maxwelldu support])

#文件末尾添加

Vim php_maxwelldu.hphp_function (MAXWELLDU);

#在文件末尾添加

Vim maxwelldu.c ' php_function (maxwelldu) {        zval *arr;               Declare a variable to accept the array parameter        HashTable *arr_hash;    Declares a hashtable variable        int array_count;        Declares an array-length variable        if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "a", &arr) ==failure) {//determines the accepted array is > no is an array , and put the value in Arr                return;        }        Arr_hash = z_arrval_p (arr); Convert the array to hashtable        Array_count = zend_hash_num_elements (Arr_hash);//Get a total of how many elements Zend by the function provided by Return_long        ( Array_count); Returns the number of elements} ' ' #然后修改zend_function_entry maxwelldu_functions[] = {The contents are "Const Zend_function_entry maxwelldu_ Functions[] = {        Php_fe (maxwelldu,null)        {null,null,null}}; "

#打包

#注意PHP的安装方式不同php-config may not be the same directory

Phpize./configure--with-php-c/bin/php-configmakemake Testmake Install

#这个时候会自动将扩展放到对应的扩展目录

#修改php的配置文件, like adding mysql,memcache and other extensions as usual

#重启apache或者php-FPM

Extension=maxwelldu.soservice httpd restartservice php-fpm Restart

#查看已经安装的扩展

Php-m

#在phpinfo里面可以查看到maxwelldu

#然后就可以在PHP脚本里面使用了

!--? Php$arr = [1, 2, 3, 4, 5];echo maxwelldu ($arr) = = Count ($arr), php_eol;//Print out 1 to indicate the number of arrays returned by the function and the coun of the system The T function returns the same value as  

Reference address:

http://blog.csdn.net/heiyeshuwu/article/details/3453854

/http www.360doc.com/content/13/1226/17/14452132_340319333.shtml

Http://www.nowamagic.net/librarys/veda/detail /1467

http://blog.csdn.net/super_ufo/article/details/3863731

http://www.phppan.com/2010/02/ php-source-12-return_value/

http://www.ccvita.com/496.html

The above describes the writing of the first PHP extension, the implementation of the number of arrays, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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