簡體   English   中英

如何在PHP的第二次調用中獲取變量值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM