![](/img/trans.png)
[英]Why can't my local PHP function see the $test variable, and how can I get its value?
[英]How can i get the variable value in my second call in PHP?
我已经使用了全局变量和静态变量来使该值在第二次调用中可用。 但这给了我同样的错误。
public function iCheckTimeStampofAjaxAndLite($Version,$Date)
{
$node = $this->getMainContext()->getSession()->getPage()->find('css', $Date);
print_r("Count:" .sizeof($node)); echo "\n";
$DateText=(string)$node->getText();
if ($Version=='Ajax'){
static $AjaxDate;
$AjaxDate=$DateText;
print_r("AjaxDate:" .$AjaxDate); echo "\n";
return;
}
else{
if ($AjaxDate==$DateText){
print_r("DateText:" .$DateText); echo "\n";
return;
}
else{
throw new Exception("Both Date are not Same");
}
}
}
1st call -> iCheckTimeStampofAjaxAndLite("Ajax","1.50pm");
2nd call -> iCheckTimeStampofAjaxAndLite("Lite","3.50pm");
Output : (Notice: Undefined variable: AjaxDate)
在课程中声明您的私人$ajaxDate
:
private $ajaxDate;
同时更改:
//...
else{
if ($AjaxDate=$DateText){
print_r("DateText:" .$DateText); echo "\n";
return;
}
//...
进入
//...
else if ($AjaxDate==$DateText){
print_r("DateText:" .$DateText); echo "\n";
return;
}
//...
您不应该为此使用static
,使用类的属性会更好。
您将得到如下所示的结果:
class FeatureContext
{
private $ajaxDate;
// ...
public function iCheckTimeStampofAjaxAndLite($Version,$Date)
{
// ...
$this->ajaxDate = ...; // set the ajax date
var_dump($this->ajaxDate); // use the ajax date
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.