スケルトン作成

概要

雛形ファイルを作成して、修正してコンパイルを実行


作成

スケルトン生成(雛形を"sample"とする)

cd C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext
c:\php\php.exe ext_skel_win32.php --extname=sample
 
Creating directory sample
Creating basic files: config.m4 config.w32 .svnignore sample.c php_sample.h CREDITS EXPERIMENTAL tests/001.phpt sample.php [done].
 
To use your new extension, you will have to execute the following steps:
 
1.  $ cd ..
2.  $ vi ext/sample/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-sample
5.  $ make
6.  $ ./php -f ext/sample/sample.php
7.  $ vi ext/sample/sample.c
8.  $ make
 
Repeat steps 3-6 until you are satisfied with ext/sample/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.
 
 

雛形を確認

C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext>cd sample
 
C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample>dir
 ドライブ C のボリューム ラベルは OS_Install です
 ボリューム シリアル番号は 9CB8-070C です
 
 C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample のディレクトリ
 
2012/08/26  08:49    <DIR>          .
2012/08/26  08:49    <DIR>          ..
2012/08/26  08:49                16 .svnignore
2012/08/26  08:49             2,034 config.m4
2012/08/26  08:49               296 config.w32
2012/08/26  08:49                 6 CREDITS
2012/08/26  08:49                 0 EXPERIMENTAL
2012/08/26  08:49             2,773 php_sample.h
2012/08/26  08:49             5,167 sample.c
2012/08/26  08:49             5,000 sample.dsp
2012/08/26  08:49               506 sample.php
2012/08/26  08:49    <DIR>          tests
               9 個のファイル              15,798 バイト
               3 個のディレクトリ  87,314,870,272 バイトの空き領域
 
C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample>
 

config.w32を編集

// $Id$
// vim:ft=javascript
 
ARG_ENABLE("sample", "enable sample support", "no");
 
if (PHP_SAMPLE != "no") {
	EXTENSION("sample", "sample.c");
}
 
 

php_sample.hを編集

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2012 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/
 
/* $Id$ */
 
#ifndef PHP_SAMPLE_H
#define PHP_SAMPLE_H
 
extern zend_module_entry sample_module_entry;
#define phpext_sample_ptr &sample_module_entry
 
#ifdef PHP_WIN32
#	define PHP_SAMPLE_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#	define PHP_SAMPLE_API __attribute__ ((visibility("default")))
#else
#	define PHP_SAMPLE_API
#endif
 
#ifdef ZTS
#include "TSRM.h"
#endif
 
PHP_MINIT_FUNCTION(sample);
PHP_MSHUTDOWN_FUNCTION(sample);
PHP_RINIT_FUNCTION(sample);
PHP_RSHUTDOWN_FUNCTION(sample);
PHP_MINFO_FUNCTION(sample);
 
PHP_FUNCTION(confirm_sample_compiled);	/* For testing, remove later. */
PHP_FUNCTION(helloworld);	// 追加
 
/* 
  	Declare any global variables you may need between the BEGIN
	and END macros here:     
 
ZEND_BEGIN_MODULE_GLOBALS(sample)
	long  global_value;
	char *global_string;
ZEND_END_MODULE_GLOBALS(sample)
*/
 
/* In every utility function you add that needs to use variables 
   in php_sample_globals, call TSRMLS_FETCH(); after declaring other 
   variables used by that function, or better yet, pass in TSRMLS_CC
   after the last function argument and declare your utility function
   with TSRMLS_DC after the last declared argument.  Always refer to
   the globals in your function as SAMPLE_G(variable).  You are 
   encouraged to rename these macros something shorter, see
   examples in any other php module directory.
*/
 
#ifdef ZTS
#define SAMPLE_G(v) TSRMG(sample_globals_id, zend_sample_globals *, v)
#else
#define SAMPLE_G(v) (sample_globals.v)
#endif
 
#endif	/* PHP_SAMPLE_H */
 
 
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
 
 

sample.cを編集

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2012 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/
 
/* $Id$ */
 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
 
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sample.h"
 
/* If you declare any globals in php_sample.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(sample)
*/
 
/* True global resources - no need for thread safety here */
static int le_sample;
 
/* {{{ sample_functions[]
 *
 * Every user visible function must have an entry in sample_functions[].
 */
const zend_function_entry sample_functions[] = {
	PHP_FE(confirm_sample_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(helloworld,  NULL)	/* 追加 */
	PHP_FE_END	/* Must be the last line in sample_functions[] */
};
/* }}} */
 
/* {{{ sample_module_entry
 */
zend_module_entry sample_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"sample",
	sample_functions,
	PHP_MINIT(sample),
	PHP_MSHUTDOWN(sample),
	PHP_RINIT(sample),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(sample),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(sample),
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */
 
#ifdef COMPILE_DL_SAMPLE
ZEND_GET_MODULE(sample)
#endif
 
/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("sample.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_sample_globals, sample_globals)
    STD_PHP_INI_ENTRY("sample.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_sample_globals, sample_globals)
PHP_INI_END()
*/
/* }}} */
 
/* {{{ php_sample_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_sample_init_globals(zend_sample_globals *sample_globals)
{
	sample_globals->global_value = 0;
	sample_globals->global_string = NULL;
}
*/
/* }}} */
 
/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(sample)
{
	/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */
 
/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(sample)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */
 
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(sample)
{
	return SUCCESS;
}
/* }}} */
 
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(sample)
{
	return SUCCESS;
}
/* }}} */
 
/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(sample)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "sample support", "enabled");
	php_info_print_table_end();
 
	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */
 
 
/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */
 
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_sample_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_sample_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;
 
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
		return;
	}
 
	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "sample", arg);
	RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/
 
 
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
 /* 追加 */
PHP_FUNCTION(helloworld)
{
    printf("Hello World\n");
    return;
}
 
 

コンパイル

cd C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample
C:\php\SDK\phpize
configure --enable-sample
nmake
 

作成モジュール確認

C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample>dir Release_TS
 ドライブ C のボリューム ラベルは OS_Install です
 ボリューム シリアル番号は 9CB8-070C です
 
 C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample\Release_TS のディレクトリ
 
2012/08/26  09:07    <DIR>          .
2012/08/26  09:07    <DIR>          ..
2012/08/26  09:07    <DIR>          php-sdk
2012/08/26  09:07            54,272 php_sample.dll
2012/08/26  09:07               916 php_sample.dll.res
2012/08/26  09:07               642 php_sample.exp
2012/08/26  09:07             1,746 php_sample.lib
               4 個のファイル              57,576 バイト
               3 個のディレクトリ  87,310,831,616 バイトの空き領域
 
C:\php-sdk\php53dev\vc9\x86\php-5.3.16-src\ext\sample>
 


最終更新:2012年08月26日 09:10