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

    }

}

我怎样才能做到这一点?

4

7 回答 7

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);
于 2013-03-15T14:01:59.583 回答
2

当你说它不起作用时......它基本上是因为$diffrent没有正确声明所以你遇到scope了问题

我的假设

  • checkRunin和都是getFare同一个类的方法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);

输出

self:$distance = 5 ; // Called TariffFareController::checkRunin :
self:$distance = 5 ; // Called TariffFareController::getFare :
于 2013-03-18T16:06:29.333 回答
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

}

}
于 2013-03-18T10:06:29.593 回答
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);

这就是@John Conde 的答案,
只是不要忘记使用静态作用域::

于 2013-03-21T16:19:33.927 回答
0

我不知道为什么上面的答案对你不起作用。但是试试这个想法
class someThing { protected $distance; 受保护的函数 initDistance() { $this->distance = 5; } protected function useDistance(){ if($this->distance < 5) { //做任何事情 } } $obj = new someThing(); $obj->initDistance(); $obj->useDistance();

于 2013-03-22T05:25:19.817 回答
-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

    }

}
于 2013-03-18T14:49:10.787 回答
-3

将距离创建为全局变量,在 checkRunIn 中设置它,然后在 getFare 中需要时访问它

于 2013-03-15T14:03:02.420 回答