2014-04-23 18 views
0

我已經使用了全局變量和靜態變量來使得第二個調用中的值可用。但它給了我同樣的錯誤。如何在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) 
+1

請學什麼[得墨忒耳定律(http://en.wikipedia.org/wiki/Law_of_Demeter)是。 –

回答

0

在你的類聲明私有$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; 
    } 
//... 
0

你不應該爲此使用static,使用Pro這個班級的情況要好得多。

你會用這樣的事情結束了:

class FeatureContext 
{ 
    private $ajaxDate; 

    // ... 

    public function iCheckTimeStampofAjaxAndLite($Version,$Date) 
    { 
     // ... 
     $this->ajaxDate = ...; // set the ajax date 

     var_dump($this->ajaxDate); // use the ajax date 
    } 
} 
+0

請說明「PHP中不存在__」。 http://www.php.net/manual/zh/language.variables.scope.php#language.variables.scope.static – Numbers

+1

@Numbers我學到了一些新東西,謝謝。從答案中刪除它 –

相關問題