I am new here but a strange well problem has me banging my head at a wall.
I’m also new in PHP, having previous experience in C# and some in C.
However the problem I have is rather basic variable handling, more explicitly assigning to a variable.
Let’s say we have a code as follows:
class PrebaciApp extends Form {
public $operation;
public function getOperation(){
return $this->operation;
}
public function setOperation($setOp){
$this->operation=$setOp;
}
public function __construct() {
$this->addWidget(new HiddenBox('hidden'));
$this->addWidget(new TextBox('text1'));
$this->addWidget(new Button('btn1', '+', 'onPlus'));
$this->addWidget(new Button('btn2', '-', 'onMinus'));
$this->addWidget(new Button('btn3', '=', 'onEq'));
}
public function onPlus() {
$this->setOperation('+');
**var_dump($this->operation);**
}
public function onMinus() {
$this->setOperation('-');
}
protected function onEqual() {
**var_dump($this->operation);**
switch ($this->operacija) {
case '+':
//some logic
case '-':
//some logic
case '*':
//some logic
}
$this->operation='';
}
}
?>
Lets assume that the first method called is onPlus or onMinus and then onEqual.
Now for some reason the var_dump in onEqual method is saying null, while the var_dump in onPlus is saying string and ‘+’ which is correct.
So my question is, why is $operation, after the methods onPlus or onMinus are over, is set to null?? Am I missing something obviouss in PHP? In Visual Studio with watching variables I could more easily discern the problem.
Edit: Hmm I don’t really know what do you mean by getting the process with which i tested this class. Since there is no watching variables here in PHP, I would just correct then run the php script. The so called F5 testing :D.
However I do have a notion of the problem, the hierarchy is PrebaciApp extends Form, which extends WidgetContainer which holds the collection (array) of Widget objects.
So that’s to much to post here.
But the methods are actually called after pressing a button on the web page itself, which is I think sent through Form or some other class and then calls PrebaciApp. I think the only logical explanation would be that the starting web page then calls another instance of PrebaciApp which has $operation on null.
Maybe I need singleton or static?
Maybe it is something in your Form class? I ran this test, and added the output. It is as I expected…
SOrry, I pasted the wrong version. Here’s the file I used:
and the output: