如何在php扩展中包装c ++类?

时间:2022-10-11 20:26:50

I created 4 files to wrap a c++ class in php Extension.Now I want to run the code for wrapping.But I have no idea about that.How can I run the code?

我创建了4个文件来包装一个c ++类在php Extension.Now我想运行包装的代码。但我不知道。我怎么能运行代码?

I'm following the tutorial over at http://devzone.zend.com/article/4486, and here's the Graph code I have so far: vehicles.cc

我正在http://devzone.zend.com/article/4486上关注这个教程,这是我到目前为止的图表代码:vehicles.cc

#include "php_vehicles.h"
#include "car.h"

zend_object_handlers car_object_handlers;

struct car_object {
    zend_object std;
    Car *car;
};
zend_class_entry *car_ce;
void car_free_storage(void *object TSRMLS_DC)
{
    car_object *obj = (car_object *)object;
    delete obj->car; 

    zend_hash_destroy(obj->std.properties);
    FREE_HASHTABLE(obj->std.properties);

    efree(obj);
}

zend_object_value car_create_handler(zend_class_entry *type TSRMLS_DC)
{
    zval *tmp;
    zend_object_value retval;

    car_object *obj = (car_object *)emalloc(sizeof(car_object));
    memset(obj, 0, sizeof(car_object));
    obj->std.ce = type;

    ALLOC_HASHTABLE(obj->std.properties);
    zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
    zend_hash_copy(obj->std.properties, &type->default_properties,
        (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));

    retval.handle = zend_objects_store_put(obj, NULL,
        car_free_storage, NULL TSRMLS_CC);
    retval.handlers = &car_object_handlers;

    return retval;
}


PHP_METHOD(Car, __construct)
{
long maxGear;
    Car *car = NULL;
    zval *object = getThis();

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxGear) == FAILURE) {
        RETURN_NULL();
    }

    car = new Car(maxGear);
    car_object *obj = (car_object *)zend_object_store_get_object(object TSRMLS_CC);
    obj->car = car;

}
PHP_METHOD(Car, shift)
{
}
PHP_METHOD(Car, accelerate)
{
Car *car;
    car_object *obj = (car_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    car = obj->car;
    if (car != NULL) {
        car->accelerate();
    }
}
PHP_METHOD(Car, brake)
{
}
PHP_METHOD(Car, getCurrentSpeed)
{
Car *car;
    car_object *obj = (car_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    car = obj->car;
    if (car != NULL) {
        RETURN_LONG(car->getCurrentSpeed());
    }
    RETURN_NULL();
}
PHP_METHOD(Car, getCurrentGear)
{
}


function_entry car_methods[] = {
    PHP_ME(Car,  __construct,     NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    PHP_ME(Car,  shift,           NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Car,  accelerate,      NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Car,  brake,           NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Car,  getCurrentSpeed, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Car,  getCurrentGear,  NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

PHP_MINIT_FUNCTION(vehicles)
{
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "Car", car_methods);
    car_ce = zend_register_internal_class(&ce TSRMLS_CC);
    car_ce->create_object = car_create_handler;
    memcpy(&car_object_handlers,
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    car_object_handlers.clone_obj = NULL;
    return SUCCESS;
}

zend_module_entry vehicles_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_VEHICLES_EXTNAME,
    NULL,        /* Functions */
    PHP_MINIT(vehicles),        /* MINIT */
    NULL,        /* MSHUTDOWN */
    NULL,        /* RINIT */
    NULL,        /* RSHUTDOWN */
    NULL,        /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
    PHP_VEHICLES_EXTVER,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_VEHICLES
extern "C" {
ZEND_GET_MODULE(vehicles)
}
#endif

php_vehicles.h

#ifndef PHP_VEHICLES_H
#define PHP_VEHICLES_H

#define PHP_VEHICLES_EXTNAME  "vehicles"
#define PHP_VEHICLES_EXTVER   "0.1"

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

extern "C" {
#include "php.h"
}

extern zend_module_entry vehicles_module_entry;
#define phpext_vehicles_ptr &vehicles_module_entry;

#endif /* PHP_VEHICLES_H */

config.m4

PHP_ARG_ENABLE(vehicles,
    [Whether to enable the "vehicles" extension],
    [  --enable-vehicles      Enable "vehicles" extension support])

if test $PHP_VEHICLES != "no"; then
    PHP_REQUIRE_CXX()
    PHP_SUBST(VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
    PHP_NEW_EXTENSION(vehicles, vehicles.cc car.cc, $ext_shared)
fi

car.h

#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H

// A very simple car class
class Car {
public:
    Car(int maxGear);
    void shift(int gear);
    void accelerate();
    void brake();
    int getCurrentSpeed();
    int getCurrentGear();
private:
    int maxGear;
    int currentGear;
    int speed;
};

#endif /* VEHICLES_CAR_H */

car.cc

#include "car.h"

Car::Car(int maxGear) {
    this->maxGear = maxGear;
    this->currentGear = 1;
    this->speed = 0;
}

void Car::shift(int gear) {
    if (gear < 1 || gear > maxGear) {
        return;
    }
    currentGear = gear;
}

void Car::accelerate() {
    speed += (5 * this->getCurrentGear());
}

void Car::brake() {
    speed -= (5 * this->getCurrentGear());
}

int Car::getCurrentSpeed() {
    return speed;
}

int Car::getCurrentGear() {
    return currentGear;
}

2 个解决方案

#1


1  

  1. Ensure that the module is loaded correctly in PHP. You can check this on the command line. There should be an entry corresponding to your extension $ php -i | grep vehicles

    确保在PHP中正确加载模块。您可以在命令行上进行检查。应该有一个对应于您的扩展名$ php -i |的条目grep车辆

  2. In PHP, you can do the following

    在PHP中,您可以执行以下操作

    <?php $oCar = new Car(); $oCar->shift(1); ?>

    移(1); ?>

See here for more info: http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

有关详细信息,请参阅此处:http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

#2


0  

You should first to compile module. Try next commands:

你应该先编译模块。尝试下一个命令:

phpize && configure && make

Into directory with you source code appears compiled so-module vehicles.so. Add that module to you php by creating corresponding ini-file with "extension=/path/to/vehicles.so" content and include them into php.ini. Than try

进入目录,你的源代码出现编译so-module vehicles.so。通过使用“extension = / path / to / vehicles.so”内容创建相应的ini文件并将它们包含到php.ini中,将该模块添加到php。比尝试

php -m | grep vehicles

If you see module name try use created class in you php code.

如果您看到模块名称,请尝试在您的PHP代码中使用创建的类。

#1


1  

  1. Ensure that the module is loaded correctly in PHP. You can check this on the command line. There should be an entry corresponding to your extension $ php -i | grep vehicles

    确保在PHP中正确加载模块。您可以在命令行上进行检查。应该有一个对应于您的扩展名$ php -i |的条目grep车辆

  2. In PHP, you can do the following

    在PHP中,您可以执行以下操作

    <?php $oCar = new Car(); $oCar->shift(1); ?>

    移(1); ?>

See here for more info: http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

有关详细信息,请参阅此处:http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

#2


0  

You should first to compile module. Try next commands:

你应该先编译模块。尝试下一个命令:

phpize && configure && make

Into directory with you source code appears compiled so-module vehicles.so. Add that module to you php by creating corresponding ini-file with "extension=/path/to/vehicles.so" content and include them into php.ini. Than try

进入目录,你的源代码出现编译so-module vehicles.so。通过使用“extension = / path / to / vehicles.so”内容创建相应的ini文件并将它们包含到php.ini中,将该模块添加到php。比尝试

php -m | grep vehicles

If you see module name try use created class in you php code.

如果您看到模块名称,请尝试在您的PHP代码中使用创建的类。