如何使用 php 扩展访问类中的变量?

发布于 2024-12-04 10:02:52 字数 6192 浏览 0 评论 0原文

我在 ubuntu 下使用 c++ 工作。我有以下示例: [汽车.h]

#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H

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

#endif /* VEHICLES_CAR_H *

/ [car.cc]

#include "car.hpp"
#include "car2.hpp"
#include "car2.cc"
#include "car3.h"
#include "car3.cc"
#include <stdio.h>
#include <iostream>
using namespace std;

Car::Car() {

}

void Car::shift(int gear) {
    if (gear < 1 || gear > maxGear) {
        return;
    }
    currentGear = gear;
}
bool Car::accelerate() {
    speed = 10;
    if (speed == 10) return true;
    else return false;
}

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

int Car::getCurrentSpeed() {
    Hello h; //this is in another class...it's working ok
    h.say_hello();
    speed=5;
    return speed;
}

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

[vehicles.cc]

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

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, "", NULL) == FAILURE) {
        RETURN_NULL();
    }

    car = new Car();
    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) {
       RETURN_BOOL( car->accelerate());
       RETURN_LONG(car->speed);
    }

}
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 

#ifdef __cplusplus
extern "C" {
    #endif
#ifdef ZTS
#include "TSRM.h"
#endif
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
    #endif
#include "php.h"

#ifdef __cplusplus
}
#endif


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

[test.php]

<html>
    <head>
        <title>MY FIRST PHP EXTENSION</title>
    </head>
    <body>
<?php


$car = new Car();
echo "FIRST RESULT: ".$car->getCurrentSpeed()." ";  // prints '0'
//echo "CAR IS: ".$car->accelerate();
if ($car->accelerate() == True) { echo "IT IS TRUE AND SPEED IS: ". $car->$speed;}

//echo "SECOND RESULT: ". $car->getCurrentSpeed()." "; // prints '5'
?>

    </body>
</html>

I have the following error at line $car->$speed:  Undefined variable: speed in /home/test.php on line 13
PHP Fatal error:  Cannot access empty property 

为什么?怎么解决这个问题呢?谢谢 !欣赏

I am working in c++ under ubuntu. I have the following example:
[car.h]

#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H

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

#endif /* VEHICLES_CAR_H *

/
[car.cc]

#include "car.hpp"
#include "car2.hpp"
#include "car2.cc"
#include "car3.h"
#include "car3.cc"
#include <stdio.h>
#include <iostream>
using namespace std;

Car::Car() {

}

void Car::shift(int gear) {
    if (gear < 1 || gear > maxGear) {
        return;
    }
    currentGear = gear;
}
bool Car::accelerate() {
    speed = 10;
    if (speed == 10) return true;
    else return false;
}

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

int Car::getCurrentSpeed() {
    Hello h; //this is in another class...it's working ok
    h.say_hello();
    speed=5;
    return speed;
}

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

[vehicles.cc]

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

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, "", NULL) == FAILURE) {
        RETURN_NULL();
    }

    car = new Car();
    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) {
       RETURN_BOOL( car->accelerate());
       RETURN_LONG(car->speed);
    }

}
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 

#ifdef __cplusplus
extern "C" {
    #endif
#ifdef ZTS
#include "TSRM.h"
#endif
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C" {
    #endif
#include "php.h"

#ifdef __cplusplus
}
#endif


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

[test.php]

<html>
    <head>
        <title>MY FIRST PHP EXTENSION</title>
    </head>
    <body>
<?php


$car = new Car();
echo "FIRST RESULT: ".$car->getCurrentSpeed()." ";  // prints '0'
//echo "CAR IS: ".$car->accelerate();
if ($car->accelerate() == True) { echo "IT IS TRUE AND SPEED IS: ". $car->$speed;}

//echo "SECOND RESULT: ". $car->getCurrentSpeed()." "; // prints '5'
?>

    </body>
</html>

I have the following error at line $car->$speed:  Undefined variable: speed in /home/test.php on line 13
PHP Fatal error:  Cannot access empty property 

WHY? How to solve this? thx ! appreciate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-12-11 10:02:52

$car->$speed 更改为 $car->getCurrentSpeed()

if ($car->accelerate() == True) { echo "IT IS TRUE AND SPEED IS: ". $car->getCurrentSpeed();}

Change $car->$speed to $car->getCurrentSpeed():

if ($car->accelerate() == True) { echo "IT IS TRUE AND SPEED IS: ". $car->getCurrentSpeed();}
淡淡離愁欲言轉身 2024-12-11 10:02:52

调用 $car->$speed 属性,您应该首先定义 $speed 变量。

speed 之前在 PHP 代码中定义它

$speed = 'speed'; // 您应该在调用 $car->$ 假设您调用 $car->speed 因为 $speed 定义为“speed”。

这是代码的 PHP 部分。

或者,如果您需要访问 $car 对象的 'speed' 属性,您可以简单地调用 $car->speed

Calling $car->$speed property you should firs define $speed variable.

$speed = 'speed'; // You should define this in PHP code before calling $car->$speed

Calling $car->$speed assumes that you call $car->speed because $speed defined as 'speed'.

It's PHP part of the code.

Or you can simply call $car->speed if you need to access the 'speed' property of $car object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文