痛定思痛: 开始了解 PHP 拓展开发,下面这篇文章不错!照着文章讲的,终于实现了!

m.php的代码

浏览器访问 m.php 文件!(备注:在linux 命令行中 php -r 'cthulhu();'  是错误的)

但在浏览器访问是可以的!!

说明: 测试 成功!!

这个链接不错: https://www.simonholywell.com/post/2010/09/15-excellent-resources-for-php-extension-development/

----------------------------------------------------------------------------------------------

https://www.kchodorow.com/blog/2011/08/11/php-extensions-made-eldrich-hello-world/

PHP Extensions Made Eldrich: Hello, World!

This is part 2 of a 4-part tutorial on writing PHP extensions:

  1. Setting Up PHP – compiling PHP for extension development
  2. Hello, world! – your first extension
  3. Working with the API – the PHP C API
  4. Classes – creating PHP objects in C

First we need to think of a name for our extension. I’ve been reading some H.P. Lovecraft, so let’s call it “rlyeh”.

For our first extension, we’ll create a new function, cthulhu(). When we call cthulhu() (tee hee), PHP will print “In his house at R’lyeh dead Cthulhu waits dreaming.”

Cheat Sheet

If you don’t want to copy/paste all of the code, you can clone the Github repo for this tutorial and check out sections as you go.

$ git clone git://github.com/kchodorow/rlyeh.git

This part of the tutorial (Hello, world!) is the master branch. Stating in part 3, each “unit” has a branch: <branchname> at the beginning of the section. You can checkout this branch if you want to see the code example in context.

For example, if you see branch: oop, you’d do:

$ git checkout -b oop origin/oop

Then you can compare what you’re doing to the “ideal” example code.

Setting Up

Create a directory for your PHP extension, named “rlyeh”. This is where all of the source code for your extension will live.

$ mkdir rlyeh
$ cd rlyeh

A PHP extension consists of at least three files:

  1. “config.m4”, which contains compilation instructions for PHP
  2. “php_extname.c”: source code
  3. “php_extname.h”: a header file

Creating a config.m4 file is wholly lacking in interest, so just cut/paste the one below.

dnl lines starting with "dnl" are comments

PHP_ARG_ENABLE(rlyeh, whether to enable Rlyeh extension, [  --enable-rlyeh   Enable Rlyeh extension])

if test "$PHP_RLYEH" != "no"; then

  dnl this defines the extension
PHP_NEW_EXTENSION(rlyeh, php_rlyeh.c, $ext_shared) dnl this is boilerplate to make the extension work on OS X
case $build_os in
darwin1*.*.*)
AC_MSG_CHECKING([whether to compile for recent osx architectures])
CFLAGS="$CFLAGS -arch i386 -arch x86_64 -mmacosx-version-min=10.5"
AC_MSG_RESULT([yes])
;;
darwin*)
AC_MSG_CHECKING([whether to compile for every osx architecture ever])
CFLAGS="$CFLAGS -arch i386 -arch x86_64 -arch ppc -arch ppc64"
AC_MSG_RESULT([yes])
;;
esac fi

If you want to call your extension something else, global replace “rlyeh” with your extension’s name.

Now for the actual extension: create a file called php_rlyeh.c with the following content:

// include PHP API
#include <php.h>
 
// header file we'll create below
#include "php_rlyeh.h"
 
// define the function(s) we want to add
zend_function_entry rlyeh_functions[] = {
PHP_FE(cthulhu, NULL)
{ NULL, NULL, NULL }
};
 
// "rlyeh_functions" refers to the struct defined above
// we'll be filling in more of this later: you can use this to specify
// globals, php.ini info, startup and teardown functions, etc.
zend_module_entry rlyeh_module_entry = {
STANDARD_MODULE_HEADER,
PHP_RLYEH_EXTNAME,
rlyeh_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
PHP_RLYEH_VERSION,
STANDARD_MODULE_PROPERTIES
};
 
// install module
ZEND_GET_MODULE(rlyeh)
 
// actual non-template code!
PHP_FUNCTION(cthulhu) {
// php_printf is PHP's version of printf, it's essentially "echo" from C
php_printf("In his house at R'lyeh dead Cthulhu waits dreaming.\n");
}

That’s a whole lotta template, but it’ll make more sense as you go along.

Learning PHP extension programming is sort of like learning Java as your first programming language: “type ‘public static void main’.” “Why? What does that even mean?” “It doesn’t matter, you’ll learn about it later.”

You also have to make a header file, to declare the cthulhu function as well as the two extension info macros used in php_rlyeh.c (PHP_RLYEH_EXTNAME and PHP_RLYEH_VERSION).

Create a new file, php_rlyeh.h, and add a couple of lines:

 
#define PHP_RLYEH_EXTNAME "rlyeh"
#define PHP_RLYEH_VERSION "0.01"
 
PHP_FUNCTION(cthulhu);

You can change the version whenever you do a new release. It can be any string. It’s displayed when you do:

$ php --ri rlyeh

(once the extension is installed).

Speaking of, now all that’s left is to compile and install. Make sure that your custom-compiled-PHP is first in your PATH. If it isn’t, put it there before doing the rest of the install.

$ echo $PATH
$PHPDIR/install-debug-zts/bin:/usr/local/bin:/usr/bin
$ phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
$
$ ./configure
# lots of checks...
$
$ make
# compile... Build complete.
Don't forget to run 'make test'. $ make install
$
Installing shared extensions: $PHPDIR/install-debug-zts/lib/php/extensions/debug-zts-20090626/

Now, add your extension to your php.ini file. PHP is probably expecting a php.ini file in the lib/ subdirectory of your install directory ($PHPDIR/install-debug-zts/lib/php.ini). It probably doesn’t exist yet, so create a new php.ini file with one line:

extension=rlyeh.so

Now you should be able to use your function from PHP without importing, loading, or requiring anything. Do:

$ php -r 'cthulhu();'
In his house at R'lyeh dead Cthulhu waits dreaming.

Your first PHP extension is working!

Next up: a deep dive into the PHP API.

PHP拓展开发的更多相关文章

  1. chrome拓展开发实战:页面脚本的拦截注入

    原文请访问个人博客:chrome拓展开发实战:页面脚本的拦截注入 目前公司产品的无线站点已经实现了业务平台组件化,所有业务组件的转场都是通过路由来完成,而各个模块是通过requirejs进行统一管理, ...

  2. chrome拓展开发实战

    chrome拓展开发实战:页面脚本的拦截注入 时间 2015-07-24 11:15:00  博客园精华区 原文  http://www.cnblogs.com/horve/p/4672890.htm ...

  3. CSV Data Set Config 拓展开发

    1.目的 在日常的性能测试项目中,经常会遇到参数化的问题,我们所熟知的LR工具对参数化支持非常友好,然而JMeter相对逊色一些.大家都知道在使用JMeter工具参数化时常用CSV Data Set ...

  4. 【包教包会】Chrome拓展开发实践

    首发于微信公众号<前端成长记>,写于 2019.10.18 导读 有句老话说的好,好记性不如烂笔头.人生中,总有那么些东西你愿去执笔写下. 本文旨在把整个开发的过程和遇到的问题及解决方案记 ...

  5. [php 拓展开发] hello world

    1.下载php源码包 2.在php-7.0.9/ext 下执行 ./ext_skel --extname=hello  3. 4.拓展代码 5. 6.编译 多了分号,出错. 重新编译成功,但是生成的文 ...

  6. 使用TypeScript拓展你自己的VS Code!

    0x00 前言 在前几天的美国纽约,微软举行了Connect(); //2015大会.通过这次大会,我们可以很高兴的看到微软的确变得更加开放也更加务实了.当然,会上放出了不少新产品和新功能,其中就包括 ...

  7. 使用TypeScript拓展你自己的VSCode

    转自:http://www.iplaysoft.com/brackets.html使用TypeScript拓展你自己的VSCode! 0x00 前言在前几天的美国纽约,微软举行了Connect(); ...

  8. 微信小程序--聊天室小程序(云开发)

    微信小程序 -- 聊天室小程序(云开发) 从微信小程序开发社区更新watch接口之后,一直在构思这个项目.项目已经完成很久,但是一直都没有空写一篇博客记录展示一下. 开源地址 wx-cloud-im: ...

  9. 前端学PHP之mysql扩展函数

    × 目录 [1]连接数据库 [2]使用数据库 [3]执行SQL查询[4]操作结果集[5]关闭连接 前面的话 mysql由于其体积小.速度快.总体拥有成本低,尤其是具有开放源码这一特点,许多中小型网站为 ...

随机推荐

  1. 【Linux命令大全】

    Linux常用命令大全 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部 ...

  2. js-dom-EventUtil

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【13】javascript跨域通信

    javascript跨域通信 同源:两个文档同源需满足 协议相同 域名相同 端口相同 跨域通信方法: 01,通过设置img,script,link,iframe元素的src,href属性为目标url. ...

  4. Oracle跟踪分析数据库启动的各个阶段

    目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...

  5. [转]pickle python数据存储

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  6. Mac下的Eclipse不能记住工作空间问题

    每次启动eclipse都要选择工作空间,即使你勾选了"选择这个作为默认"也不行. Eclipse版本 4.5, mac os版本10.12 找到这个目录下的config.ini文件 ...

  7. P2341 受欢迎的牛

    受欢迎的牛 洛谷链接 题目大意: 有n头牛,牛会喜欢其他牛和自己,而喜欢是可以传递的,求被所有牛喜欢的牛的数量. 思路: 这是一道tarjan缩点的题目.被所有牛都喜欢的牛,一定会在一个强连通分量里. ...

  8. tomcat的安装和优化

    tomcat的安装 jdk版本安装 #!/bin/bash # desc: jdk安装脚本1. 1.7 1.8 download_url='http://**************' jdk_env ...

  9. HDU——2473Junk-Mail Filter(并查集删点)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  10. 黑匣子_NOI导刊2010提高 (对顶堆)

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...