如何使用php扩展从类访问变量? 我在Ubuntu下工作在C++中。我举了以下例子: [car.h]

如何使用php扩展从类访问变量? 我在Ubuntu下工作在C++中。我举了以下例子: [car.h],php,c++,php-extension,php-internals,Php,C++,Php Extension,Php Internals,/ [car.cc] [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 #i

/ [car.cc]

[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 

我的第一个PHP扩展
我在第13行的$car->$speed处有以下错误:未定义变量:speed in/home/test.php
PHP致命错误:无法访问空属性

为什么??如何解决这个问题?谢谢!感谢将
$car->$speed
更改为
$car->getCurrentSpeed()


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

$speed='speed'
//在调用
$car->$speed

调用
$car->$speed
假设调用$car->speed,因为$speed定义为“speed”

这是PHP代码的一部分


或者,如果需要访问$car对象的“speed”属性,您可以简单地调用
$car->speed
,如何定义速度变量?请根据我所写的内容给我举个例子,在vehicles.cc中,在哪里添加这个?你能说得更具体些吗?我是phpi的新手,使用了$car->speed,但不起作用。我有个错误:unDealth:Car::$SPEEDI认为您应该在C++类中定义公共的“速度”属性。然后通过$car->speedint speed is public访问它。或者你是什么意思?如果我想打印速度-变量而不是函数呢。如何解决这个问题?谢谢!该方法获取当前速度值(从C++类)。我不相信直接从PHP访问C++变量是可能的。您需要在C++类中使用GETER和Stter方法来访问PHP中的变量。您的
getCurrentSpeed
方法应该只包含
返回速度。我明白了。因此,如果我想打印Car类中的所有变量,有没有可能在一个函数中打印所有变量?(如何在单个方法中打印speed、currentGear和maxGear)?从PHP调用所有getter方法并回显返回的值。另外,添加一个新的C++方法,返回一个字符串到PHP,包含所有要回音的内容。QBT我想问你:如果类车在类机器里面,比如:类机器{class Car…};我应该在vehicles.cc代码中更改什么?请帮忙
  #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 */
    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
<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 
if ($car->accelerate() == True) { echo "IT IS TRUE AND SPEED IS: ". $car->getCurrentSpeed();}