PHP extension development with Linux and Eclipse CDT. Developing basic extension with ext_skel

Before you start you should setup your developing environment. How to setup it is explained in one of my previous posts - http://cmyker.blogspot.com/2011/11/php-extension-development-with-linux.html

My platform is Opensuse 12.2, PHP 5.3.22, Eclipse CDT 8.0.2.
If you are experienced in other Linux environments you're welcome to add HOWTOs to the comments.

To make extension development process easier to start PHP source code distribution has a nice ext_skel shell script. This script will build your extension skeleton, actually fully functional extension. It is located in /usr/src/packages/BUILD/php-5.X.X/ext directory. To build your new "helloworld" extension skeleton run:

# cd /usr/src/packages/BUILD/php-5.3.22
#  ./ext_skel --extname=helloworld
Creating directory helloworld
Creating basic files: config.m4 config.w32 .svnignore helloworld.c php_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt helloworld.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/helloworld/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-helloworld
5.  $ make
6.  $ ./php -f ext/helloworld/helloworld.php
7.  $ vi ext/helloworld/helloworld.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/helloworld/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.




ext_skel suggests to run additional steps to configure your extension. Lets follow this steps:

# cd..
# mcedit ext/helloworld/config.m4

PHP build system has 2 options for enabling extension - PHP_ARG_WITH and PHP_ARG_ENABLE. Right now we are interested in PHP_ARG_WITH because it has an option to build extension as a static not shared. Statically linked extension is much easier to debug (no problems with GDB breakpoints, no need to enable extension in ini). Uncomment this lines (by removing dnl at the begging of the line):

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


Next run buildconf with --force parameter to reset cache:

# ./buildconf --force
Forcing buildconf
...

In this manual we will build extension only for cli PHP binary.

# cd /usr/src/packages/SPECS

Open php5.spec and find Build cli \ - this is a call to the Build() function. Scroll down to the --disable-cgi  line and before it insert --with-helloworld=static \

Next command will fully rebuild PHP (skipping %prep stage which would delete /usr/src/packages/BUILD/php-5.X.X/ directory) according to the spec file:

# rpmbuild --short-circuit -bc php5.spec

Open CDT, refresh your project. Right click on the build-cli/sapi/cli/php file select Debug As\Local C/C++ Application , click on it and debug session should start. Stop it. Go to Run\Debug Configurations , select C/C++ Application/php , select Arguments tab add to the program arguments section /tmp/debug.php . PHP binary we debug will read your system's configuration files so if you have troubles with currently installed extensions or wish to disable all other extensions and run PHP with default configuration add -n switch before /tmp/debug.php
Create /tmp/debug.php file with contents below:

<?php
echo confirm_helloworld_compiled("helloworld");


Run again a debug session from CDT menu Run\Debug. CDT should now open sapi/cli/php_cli.c file and break execution on the main function. Open your extension file ext/helloworld/helloworld.c and set a breakpoint somewhere to make sure everything is configured correctly. For example on
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {



Continue execution and you should see in the CDT console:

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

Comments

Popular posts from this blog

Memory efficient array permutation in PHP 5.5 using generators

How to dump http request headers with PHP under CGI/FastCGI SAPI

Zend Framework 2 AJAX: return JSON response from controller action. The proper way.