2013-03-15 130 views
0

我有以下功能:變量函數

class TariffFareController { 

public static function checkRunin($fieldPickup) { 

    $runInThreshold = 5; 
    $fieldDestination = 6; 

    if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) 

    { 

     if ($fieldPickup > $fieldDestination) 

     { 

      $distance = $fieldPickup - $runInThreshold; 

     } else { 

      $distance = $fieldDestination - $runInThreshold; 

     } 


     } 
} 

我想通過$distance這個功能: -

private static function getFare($int_terminate) { 

    //CODE 
    //eg// if($distance > 5) { 

     //do something 

    } 

} 

我怎樣才能做到這一點?

+1

調用函數並將其作爲參數傳遞 – 2013-03-15 14:02:03

+0

您是否想要「傳遞$距離」是什麼意思?你會在checkRunin中調用getFare嗎? – SomeSillyName 2013-03-15 14:02:13

+0

我想在getFare函數中使用$ distance,所以我可以做類似if($ distance> 5){// DO SOMETHING} – nsilva 2013-03-15 14:20:49

回答

2

當你說,它不工作...這主要是因爲$diffrent未正確申報,以便您有scope發出

我的假設

  • 兩個checkRuningetFare是同一類的方法TariffFareController
  • $int_terminate$distance
不同

從你的類

  • $distance未聲明爲static
  • getFare試圖訪問$distance尚未宣佈

簡單的解決方案

class TariffFareController { 
    private static $distance = 0; 

    public static function checkRunin($fieldPickup) { 
     $runInThreshold = 5; 
     $fieldDestination = 6; 
     if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) { 
      if ($fieldPickup > $fieldDestination) { 
       self::$distance = $fieldPickup - $runInThreshold; 
      } else { 
       self::$distance = $fieldDestination - $runInThreshold; 
      } 
     } 
     printf("self:\$distance = %d ; // Called %s :\n", self::$distance, __METHOD__); 
    } 

    public static function getFare($int_terminate) { 
     printf("self:\$distance = %d ; // Called %s :\n", self::$distance, __METHOD__); 
    } 
} 

TariffFareController::checkRunin(10); 
TariffFareController::getFare(2); 

Outpu噸

self:$distance = 5 ; // Called TariffFareController::checkRunin : 
self:$distance = 5 ; // Called TariffFareController::getFare : 
5

return從第一函數的值:

public static function checkRunin($fieldPickup) { 
    $runInThreshold = 5; 
    $fieldDestination = 6; 

    if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) 
    { 
     if ($fieldPickup > $fieldDestination) 
     { 
      $distance = $fieldPickup - $runInThreshold; 
     } else { 
      $distance = $fieldDestination - $runInThreshold; 
     } 
    } 
    return $distance; 
} 

// Get $distance from checkRunin() 
$distance = $obj->checkRunin($fieldPickup); 
// Pass $distance as a parameter 
$obj->getFare($distance); 
+0

只有在該函數有意義時才返回第一個函數中的變量'$ distance'。 'checkRunin'看起來不那麼明顯,它會返回某種距離給我。 – Shoe 2013-03-15 14:08:44

+0

這似乎並沒有爲我工作,我只是得到了幾個錯誤... – nsilva 2013-03-18 09:47:36

-3

創建距離作爲一個全局變量,在checkRunIn時,然後訪問它,當你需要它getFare

+0

這只是錯誤的這麼多的水平... – 2013-03-15 14:37:52

1
class TariffFareController { 

public static $distance = 0; 
public static function checkRunin($fieldPickup) { 

$runInThreshold = 5; 
$fieldDestination = 6; 

if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) 

{ 

    if ($fieldPickup > $fieldDestination) 

    { 

     self::$distance = $fieldPickup - $runInThreshold; 

    } else { 

     self::$distance = $fieldDestination - $runInThreshold; 

    } 


    } 
} 

然後

設置
private static function getFare($int_terminate) { 

//CODE 
//eg// if(self::$distance > 5) { 

    //do something 

} 

} 
+0

這很好,謝謝你。但是,在其他一些函數中,我現在有一個錯誤,因爲我返回的票價如下:return self :: getFare(0);'因此它期望參數$ fieldPickup。我有什麼要改變這個? - 道歉,我相當新的PHP – nsilva 2013-03-18 10:44:18

+0

對不起,請你詳細說明這個問題,我不知道我跟着。你有兩個靜態方法checkRunin($ fieldPickup)和getFare($ int_terminate)getFare期望的是$ fieldPickup? – 2013-03-18 11:12:28

+0

另外作爲一個注意事項,我不知道如果創建所有這些方法和參數作爲靜態是嘗試做你看起來像你試圖做的最好的方式。雖然我明白我們不會在stackoverflow上獲得問題和解決方案的整個範圍。謝謝 – 2013-03-18 11:14:12

-1

會不會使用會話如下: -

session_start(); 

public static function checkRunin($fieldPickup) { 

    $runInThreshold = 5; 
    $fieldDestination = 6; 
    $_SESSION['runin_distance'] = 0; 

    if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) { 

     if ($fieldPickup > $fieldDestination) { 

      $_SESSION['runin_distance'] = $fieldPickup - $runInThreshold; 


     } else { 

      $_SESSION['runin_distance'] = $fieldDestination - $runInThreshold; 

     } 

    } 

} 

然後......

private static function getFare($int_terminate) { 

    if($_SESSION['runin_distance'] > 5) { 

    //do something 

    } 

} 
0

public static function checkRunin($fieldPickup) {
$runInThreshold = 5;
$fieldDestination = 6;

if ($fieldPickup > $runInThreshold && $fieldDestination > $runInThreshold) 
{ 
    if ($fieldPickup > $fieldDestination) 
    { 
     $distance = $fieldPickup - $runInThreshold; 
    } else { 
     $distance = $fieldDestination - $runInThreshold; 
    } 
} 
return $distance; 

}

// Get $distance from checkRunin()
$distance = $obj::checkRunin($fieldPickup);
// Pass $distance as a parameter
$obj::getFare($distance);

是從@約翰孔德
答案只是不要忘了靜態範圍使用::

0

我不知道爲什麼上面你沒有工作了答案。但試試這個想法
class someThing {protected $ distance;保護函數initDistance(){$ this-> distance = 5; } protected function useDistance(){if($ this-> distance < 5){// do something}} $ obj = new someThing(); $ obj-> initDistance(); $ obj-> useDistance();