doudiao2335 2014-08-19 17:35
浏览 37
已采纳

类中未定义的变量

I'm using a raspberry pi to control lightning in my apartment, and currently I'm writing a class to control RGB Leds. However, I'm having some troubles with it, and this particular problem is going to drive me crazy.

Whenever I call a function, setRed() for example, I get a notice that says Notice: Undefined variable: GPIORed in M:\wamp\www\LedControl\class.rgbcontrol.php on line 16

Shortened version of my class looks like this:

class RGBControl {

    var $GPIORed;
    var $GPIOGreen;
    var $GPIOBlue;

    public function __construct($red,$green,$blue) {
      $this->GPIORed = $red;
      $this->GPIOGreen = $green;
      $this->GPIOBlue = $blue;
    }


    public function setRed($power){
        shell_exec("echo \"$GPIORed=$power\" > /dev/pi-blaster");
    }

    public function setGreen($power){
        shell_exec("echo \"$GPIOGreen=$power\" > /dev/pi-blaster");
    }

    public function setBlue($power){
        shell_exec("echo \"$GPIOBlue=$power\" > /dev/pi-blaster");
    }
}

So here's basic usage, I'm defining the class with arguments 24,23,18 (the GPIO pins on the Raspberry Pi that my leds are connected to).

$LED = new RGBControl (24,23,18);

$LED->setRed($color[0] / 255);
$LED->setGreen($color[1] / 255);
$LED->setBlue($color[2] / 255);

But what am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dongxiji0687 2014-08-19 17:40
    关注

    The lines like this:

    shell_exec("echo \"$GPIOBlue=$power\" > /dev/pi-blaster");
    

    are trying to resolve variables named $GPIOBlue and $power, but $GPIOBlue doesn't exist. You need to use $this to access instance member variables. For example,

    shell_exec("echo \"{$this->GPIOBlue}=$power\" > /dev/pi-blaster");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。