我有以下功能:
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
}
}
我该怎么做?
答案 0 :(得分: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);
答案 1 :(得分: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 :
答案 2 :(得分: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
}
}
答案 3 :(得分: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的答案 只是不要忘记静态范围使用::
答案 4 :(得分:0)
我不知道为什么上面的答案对你不起作用。但试试这个想法 class someThing {protected $ distance;受保护的函数initDistance(){$ this-> distance = 5; } protected function useDistance(){if($ this-> distance< 5){// do anything}} $ obj = new someThing(); $ obj-> initDistance(); $ obj-> useDistance();
答案 5 :(得分:-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
}
}
答案 6 :(得分:-3)
将距离创建为全局变量,在checkRunIn中设置它,然后在getFare需要时访问它