Home  >  Article  >  Backend Development  >  Write the first PHP extension to calculate the number of arrays

Write the first PHP extension to calculate the number of arrays

WBOY
WBOYOriginal
2016-08-08 09:20:35768browse

Requirements: Write the first PHP extension, which contains a function called maxwelldu. maxwelldu can calculate the length of an array (same as count)

Requirements: Understand C/C++ programming and be familiar with PHP programming

System: CentOS6.5

Environment: LNMP (yum installation)

To take the first step and start writing PHP extensions, we will use a tool, and this tool is in the PHP source code, so we download a PHP source code, http://php.net /downloads.php

cd ~
mkdir software
cd software
wget http://cn2.php.net/distributions/php-5.6.11.tar.gz
tar zxvf php-5.6.11.tar.gz
cd php-5.6.11/ext

#Create an extension project. After the creation is completed, there will be an additional sayhello folder in the ext directory. This folder is our extension project

./ext_skel --extname=maxwelldu
cd maxwelldu
vim config.m4

#Open permission and remove the dnl and [ before the PHP_ARG_ENABLE line. --enable-maxwelldu ] The dnl in front of this line

PHP_ARG_ENABLE(maxwelldu, whether to enable maxwelldu support,
dnl Make sure that the comment is aligned:
[  --enable-maxwelldu           Enable maxwelldu support])

#Add

vim php_maxwelldu.h

PHP_FUNCTION(maxwelldu);

at the end of the file #Add

vim maxwelldu.c
```
PHP_FUNCTION(maxwelldu){
        zval *arr;               //声明一个变量来接受数组参数
        HashTable *arr_hash;    //声明一个HashTable的变量
        int array_count;        //声明一个数组长度的变量
        if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr)==FAILURE){ //判断接受的数组是>否是数组, 并把值放入arr中
                return;
        }
        arr_hash = Z_ARRVAL_P(arr); //将数组转换成HashTable
        array_count = zend_hash_num_elements(arr_hash);//通过zend提供的函数获取一共有多少个元素
        RETURN_LONG(array_count); //返回元素的个数
}
```

#然后修改zend_function_entry maxwelldu_functions[] = { 的内容如下
```
const zend_function_entry maxwelldu_functions[] = {
        PHP_FE(maxwelldu,NULL)
        {NULL,NULL,NULL}
};
```

at the end of the file #Package

#Note that the directory of php-config may be different depending on how PHP is installed

phpize
./configure --with-php-c/bin/php-config
make
make test
make install

#At this time, the extension will be automatically placed in the corresponding extension directory

#Modify the php configuration file, just like adding mysql, memcache and other extensions normally

#Restart apache or php-fpm

extension=maxwelldu.so
service httpd restart
service php-fpm restart

#View what has been installed Extension

php -m

#You can view maxwelldu

# in phpinfo and then you can use it in PHP scripts

<?php
$arr = [
	1, 2, 3, 4, 5
];
echo maxwelldu($arr) == count($arr), PHP_EOL; //打印出1就表示函数返回的数组个数和系统的count函数返回值一样

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 introduces the writing of the first PHP extension to calculate the number of arrays, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn