简体   繁体   English

如何在PHP的第二次调用中获取变量值?

[英]How can i get the variable value in my second call in PHP?

I have used both Global and Static variables to make the value available in second call . 我已经使用了全局变量和静态变量来使该值在第二次调用中可用。 but it gives me same error . 但这给了我同样的错误。

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)

Declare your private $ajaxDate in your class: 在课程中声明您的私人$ajaxDate

private $ajaxDate;

Also change: 同时更改:

//...

else{
  if ($AjaxDate=$DateText){
    print_r("DateText:" .$DateText); echo "\n";
          return;
  }
//...

into 进入

//...

else if ($AjaxDate==$DateText){
    print_r("DateText:" .$DateText); echo "\n";
          return;
  }
//...

You should not use static for this, using properties of the class is much nicer. 您不应该为此使用static ,使用类的属性会更好。

You'll end up with something like this: 您将得到如下所示的结果:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM