search
HomeBackend DevelopmentPHP TutorialPHP extension development-code example sharing for LINUX environment


LINUX The steps to develop PHP extensions in the environment are as follows:

1. Download the PHP source code and unzip it. My unzipped directory is: /root /lamp/php-5.5.37

2. cd to the /root/lamp/php-5.5.37/ext directory and create the file test_extension.def file

int a(int x, int y)string b(string str, int n)

3. Through extension FrameworkGeneratorGenerate framework directory:
ext_skel –extname=test_extension –proto=test_extension.def
The successful generation results are as follows:

Creating directory test_extension
awk: /root/lamp/php-5.5.37/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .svnignore test_extension.c php_test_extension.h CREDITS EXPERIMENTAL tests/001.phpt test_extension.
php [done].To use your new extension, you will have to execute the following steps:
1.  $ cd ..
2.  $ vi ext/test_extension/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-test_extension
5.  $ make
6.  $ ./sapi/cli/php -f ext/test_extension/test_extension.php
7.  $ vi ext/test_extension/test_extension.c
8.  $ make
Repeat steps 3-6 until you are satisfied with ext/test_extension/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

4. Switch to the generated framework directory: cd test_extension
5. Modify the configuration file config.m4 and remove the first lines 10, 11 and 12. The dnl is as follows

PHP_ARG_WITH(test_extension, for test_extension support,
Make sure that the comment is aligned:
[  --with-test_extension             Include test_extension support])

6. To implement the functions of functiona and b, vi test_extension.c, the modified functions a and b are as follows

PHP_FUNCTION(a)
{        
int argc = ZEND_NUM_ARGS();        
long x;        
long y;        
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
        {
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        RETURN_LONG(x + y);
}

PHP_FUNCTION(b)
{        
char *str = NULL;        
int argc = ZEND_NUM_ARGS();        
int str_len;        
long n;        
char *result;        
char *ptr;        
int result_length;        
if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        {
        
                php_error(E_WARNING, "zend_parse_parameters failure!");                
                return;
        }
        result_length = str_len * n;
        result = (char *) emalloc(result_length + 1);
        ptr = result;        while (n--) {
                memcpy(ptr, str, str_len);
                ptr += str_len;
        }
        *ptr = '/0';
        RETURN_STRINGL(result, result_length, 0);

}

7.test_extension directory Execute under: /usr/local/bin/phpize

Configuring for:
PHP Api Version:         
20121113Zend Module Api No:      
20121212Zend Extension Api No:   
220121212

8. Configuration: ./configure –with-php-config=/usr/local/bin/php-config
9. Compile: make
10. Installation: make install
After the installation is completed, test_extension.so will be generated under /usr/local/lib/php/extensions/no-debug-zts-20121212/

11. Modify php .in, add: extension=test_extension.so

The above is the detailed content of PHP extension development-code example sharing for LINUX environment. For more information, please follow other related articles on the PHP Chinese website!

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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version