使用SWIG将C ++类集成到PHP中

时间:2011-12-31 15:46:57

标签: php c++ swig

我正在使用SWIG将C ++类集成到PHP中,但由于某种原因,该类在PHP中无法正确显示,即使函数确实正确显示。这是Mac OS X 10.5.8上的SWIG 2.0.4和PHP 5.3.6。以下是用于构建库的代码和命令:

$ cat example.i
%{
#include <stdio.h>
#include "example.h"
int Example::foo(void) {
    printf("This is foo\n");
    return 0;
}
%}
%module example

class Example {
public:
    int foo(void);
};
$ cat example.h
class Example {
public:
    int foo(void);
};
$ make ehphp
/opt/local/bin/swig -c++ -php5 example.i
g++ `php-config --includes` -fPIC -c example_wrap.cpp -o example_wrap.o
g++ -dynamiclib -Wl,-flat_namespace,-U,_zend_register_resource,-U,_zend_rsrc_list_get_rsrc_type,-U,_zend_wrong_param_count,-U,_compiler_globals,-U,_zend_hash_find,-U,_gc_remove_zval_from_buffer,-U,_zend_register_long_constant,-U,_zend_get_constant,-U,__efree,-U,__emalloc,-U,_zend_error,-U,__zend_list_find,-U,_main,-U,__zend_get_parameters_array_ex,-U,_zend_register_list_destructors_ex,-U,__zval_copy_ctor_func,-U,__convert_to_string,-U,__zend_hash_add_or_update,-U,_executor_globals,-U,_zval_used_for_init,-U,_zval_is_true,-U,__object_init,-U,_php_strtolower,-U,__estrndup,-U,__object_init_ex,-U,_zend_lookup_class -o ehphp.dylib example_wrap.o
sudo cp ehphp.dylib /opt/local/lib/php/extensions/no-debug-non-zts-20090626/ehphp.dylib
$ php -a
Interactive shell

php > var_dump(dl("ehphp.dylib"));
bool(true)
php > var_dump(function_exists("bar"));
bool(true)
php > var_dump(class_exists("Example"));
bool(false)
php > 

1 个答案:

答案 0 :(得分:1)

我相信你应该

require "Example.php";

加载代理类。 (该php文件由SWIG生成。)