>  기사  >  백엔드 개발  >  PHP 확장의 Hello World에 대한 자세한 소개

PHP 확장의 Hello World에 대한 자세한 소개

黄舟
黄舟원래의
2017-08-14 09:26:031966검색


머리말

오랫동안 쓰고 싶었던 메모입니다. PHP 확장 기능 작성에 관한 글이 수백 개 있지만 그 중 상당수가 아주 오래된 것입니다. 일부 예는 작동하지 않습니다. 좀 당황스럽네요.
본 글은 자신의 메모를 메모로 기록하는데 사용됩니다.

Text

1. PHP 설치 패키지 다운로드

다운로드 주소: php 다운로드 빠른 링크
이 글에서는 php-5.6.7 설치 패키지를 선택합니다.
그런 다음 PHP를 설치하세요.

2. 확장 스켈레톤 만들기

//跑到ext目录cd php-5.6.7/ext///执行一键生成骨架的操作./ext_skel --extname=helloworld

생성 결과를 설명하는 다음 프롬프트가 표시되면
PHP 확장의 Hello World에 대한 자세한 소개

cd helloworld
ls

다음 파일을 찾을 수 있습니다.

config.m4  config.w32  CREDITS  EXPERIMENTAL  helloworld.c  helloworld.php  php_helloworld.h  tests

3.전에 확장 구성 파일 config.m4

를 수정하세요. 다음 코드 dnl을 제거합니다. (dnl은 PHP의 //와 동일합니다)

##动态编译选项,通过.so的方式链接,去掉dnl注释PHP_ARG_WITH(helloworld, for helloworld support,
Make sure that the comment is aligned:
[  --with-helloworld             Include helloworld support])##静态编译选项,通过enable来启用,去掉dnl注释PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,
Make sure that the comment is aligned:
[  --enable-helloworld           Enable helloworld support])

일반적으로 둘 중 하나를 선택할 수 있습니다(개인 취향에 따라 다르므로 이 튜토리얼에서는 활성화 주석을 제거해야 합니다).

4. 컴파일 테스트를 수행합니다

phpize
./configure --enable-helloworldmakemake install

그런 다음 php.ini에 확장을 추가합니다

vim /usr/local/php/etc/php.ini// 添加扩展extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"extension = "helloworld.so"// 重启php-fpm/etc/init.d/php-fpm restart

확장이 작성된 폴더로 돌아가서 테스트 명령을 실행합니다

php -d enable_dl=On myfile.php

다음 단어를 보면 승리가 멀지 않았다는 뜻입니다. :

confirm_helloworld_compiled

Congratulations! You have successfully modified ext/helloworld/config.m4. Module helloworld is now compiled into PHP.

confirm_helloworld_compiled ext_skel에 의해 자동으로 생성되는 테스트 함수입니다.

ps: 두 개의 PHP 버전이 로컬에 설치되어 있고 확장 기능이 php7로 작성된 경우 다음과 같은 문제가 발생할 수 있습니다.

/mydata/src/php-7.0.0/ext/helloworld/helloworld.c: 在函数‘zif_confirm_helloworld_compiled’中:
/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 错误:‘zend_string’未声明(在此函数内第一次使用)
/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 错误:所在的函数内也只报告一次。)
/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: 错误:‘strg’未声明(在此函数内第一次使用)

원인: 컴파일 환경이 php7이 아닙니다.
해결책: php5에는 zend_string 유형이 없습니다. 이를 char로 바꾸거나 PHP 버전 환경을 php7

5로 수정하세요. helloworld 함수를 생성하세요

helloworld.c를 편집하고 구현할 함수를 추가하세요

##zend_function_entry helloworld_functions 补充要实现的函数const zend_function_entry helloworld_functions[] = { 
    PHP_FE(confirm_helloworld_compiled, NULL)       /* For testing, remove later. */
    PHP_FE(helloworld,  NULL)       /* 这是补充的一行,尾巴没有逗号 */
    PHP_FE_END  /* Must be the last line in helloworld_functions[] */};

"PHP_FUNCTION"을 찾으세요. (confirm_helloworld_compiled)", 다른 함수를 시작하고 함수 엔터티를 작성합니다.

PHP_FUNCTION(helloworld) {    php_printf("Hello World!\n");
    RETURN_TRUE;
}

컴파일을 다시 진행합니다.

./configure --enable-helloworld && make && make install

정말 성공했는지 테스트합니다.

php -d enable_dl=On -r "dl('helloworld.so');helloworld();"//输出Hello World!

성공!

위 내용은 PHP 확장의 Hello World에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.