How to Make a PHP Extension

How Do I Build My First PHP Extension in C on Linux GCC?

extension for this example.

<?php
function hello_world() {
return 'Hello World';
}
?>
### config.m4

PHP_ARG_ENABLE(hello, whether to enable Hello
World support,
[ --enable-hello Enable Hello World support])
if test "$PHP_HELLO" = "yes"; then
AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
### php_hello.h

#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"

PHP_FUNCTION(hello_world);

extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry

#endif
#### hello.c

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_hello.h"

static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
{NULL, NULL, NULL}
};

zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif

PHP_FUNCTION(hello_world)
{
RETURN_STRING("Hello World", 1);
}

Building Your Extension
$ phpize
$ ./configure --enable-hello
$ make

After running each of these commands, you should have a hello.so

extension=hello.so to your php.ini to trigger it.

 php -r 'echo hello_world();'

you are done .;-)

read more here

for Easy way to just try zephir-lang to build php extension with less knowledge of

namespace Test;

/**
* This is a sample class
*/
class Hello
{
/**
* This is a sample method
*/
public function say()
{
echo "Hello World!";
}
}

compile it with zephir and get test extension

Building custom PHP extension (.so)

I recommend to read this article:

Extension Writing Part I: Introduction to PHP and Zend

and here's how to compile:

UNIX: Compiling PHP Extensions

Build PHP extensions with SWIG

(I recommend SWIG like mario said)

Read more on Canadian zip codes at Postal codes in Canada since not all letters are being used.

How to build a php extensions from tgz file using make?

Here is a link on how to build mongodb.so:
https://www.php.net/manual/en/mongodb.installation.manual.php

git clone https://github.com/mongodb/mongo-php-driver.git
cd mongo-php-driver
git submodule update --init
phpize
./configure
make all
sudo make install

You can also unpack the mongodb-1.10.0.tgz file with:

tar -xvf mongodb-1.10.0.tgz
phpize
make all
sudo make install

writing php extension that uses external shared library

Resolved this issue by adding PHP_SUBST(FOO_SHARED_LIBADD) after PHP_EVAL_LIBLINE($LIBFREETYPE2_LIBS, FOO_SHARED_LIBADD):

PHP_ARG_WITH([foo],
[for foo support],
[AS_HELP_STRING([--with-foo],
[Include foo support])])

if test "$PHP_FOO" != "no"; then
PKG_CHECK_MODULES([LIBFREETYPE2], [freetype2 >= 23.4.17])
PHP_EVAL_INCLINE($LIBFREETYPE2_CFLAGS)
PHP_EVAL_LIBLINE($LIBFREETYPE2_LIBS, FOO_SHARED_LIBADD)
PHP_SUBST(FOO_SHARED_LIBADD)
PHP_NEW_EXTENSION(foo, foo.c, $ext_shared)
fi


Related Topics



Leave a reply



Submit