Extending PHP with C++

Extending PHP with C++?

I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.

Update
As @therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.

How to extend PHP with C++?

Yes, you can do so by writing a PHP extension.

See PHP at the Core: A Hacker's Guide to the Zend Engine.

PHP using c++ extension will be faster?

Yes, using a C++ extension for complex calculations will give you a significant speedup over PHP.

Check out these questions for more details to go about doing this:

  • How can I use C++ code to interact with PHP?
  • Extending PHP with C++?

extend php with java/c++?

Most of PHP is written in modular C code. You can create your own PHP extensions in C. See http://php.net/internals, the PHP wiki and the book "Extending and Embedding PHP" by Sara Golemon.

PHP Extensions in C: Can you inherit a class in one extension from another separate extension?

I figured it out on my own, yes you can extend from other extensions, you have to make sure that classes in other extensions are exported with PHPAPI and that the other extension is always loaded before yours.

 extern PHPAPI zend_class_entry *class_entry_var; 

**:EDIT if using below code then using PHPAPI is not necessary

and then you would refer to it in your class with

 zend_register_internal_class_ex(&ce, NULL , "ClassName" TSRMLS_CC);

Extending a class with a trait

It is possible, the choice is a discretion of the developer, the best solution is based on experience.

trait C {
public function hello() {
parent::a();
echo 'World!';
}
}

class B {
function a() {
echo "hello";
}
}

class A extends B{
use C;
}

(new A())->hello(); // helloWorld!

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



Related Topics



Leave a reply



Submit