0

I have the following function:

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;

        }


       }
}

I want to pass $distance to this function:-

private static function getFare($int_terminate) {

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

        //do something

    }

}

How can I do this?

5
  • 1
    Call the function and pass it as a parameter Commented Mar 15, 2013 at 14:02
  • What do you mean by you want to "pass $distance"? Will you be calling getFare from within checkRunin? Commented Mar 15, 2013 at 14:02
  • I want to use $distance in the getFare function, so I can do something like if($distance > 5) { //DO SOMETHING } Commented Mar 15, 2013 at 14:20
  • @nsilva can you not declare $distance as a static variable and set it and access it as so from each of your methods? Commented Mar 18, 2013 at 10:03
  • Ahh that's easy... Are you accessing these via any instance or static methods? Commented Mar 18, 2013 at 10:09

7 Answers 7

5

return the value from the first function:

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Return the variable $distance in the first function only if it makes sense for that function to do that. checkRunin doesn't seems so obvious it will return some kind of a distance to me.
2
+50

When you say its does not work ... it basically because $diffrent was not properly declared so you are having scope issues

My Assumption

  • Both checkRunin and getFare are method of the same class TariffFareController
  • $int_terminate is different from $distance

From your class

  • $distance not declared as static
  • getFare is trying to access $distance that has not been declared

Simple Solution

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);

Output

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

Comments

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;

    }


   }
}

then

private static function getFare($int_terminate) {

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

    //do something

}

}

7 Comments

That's great thank you. However, in some of the other functions I now have an error because I am returning the fare as follows return self::getFare(0); and so it is expecting the parameter $fieldPickup. What would I have to change this to? - do apologise, i'm fairly new to PHP
sorry could you please elaborate on the problem, I'm not sure I follow. you have two static methods checkRunin($fieldPickup), and getFare($int_terminate) what in getFare is expecting $fieldPickup?
Also as a matter of note, i'm not sure if creating all these methods and parameters as static is the best way of trying to do what it seems like you are trying to do. Although I understand that we dont get the whole scope of the problem and solution on stackoverflow. Thanks
sorry ignore the bit above. I've done as you have said, if I echo(self::$runindistance) I get the value within the checkRunin function, however, if I echo(self::$runindistance) within the getFare function, the variable is null. Any ideaS?
$runindistance is declared where and how? have you substituted distance for $runindistance?
|
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);

that is the answer from @John Conde
just don't forget the static scope use ::

Comments

0

I dont know why the answer above didnt work for you. But try this idea
class someThing { protected $distance; protected function initDistance() { $this->distance = 5; } protected function useDistance(){ if($this->distance < 5) { //do anything } } $obj = new someThing(); $obj->initDistance(); $obj->useDistance();

Comments

-1

Would it not be possible to use sessions as follows:-

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;

        }

    }

}

and then...

private static function getFare($int_terminate) {

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

    //do something

    }

}

Comments

-3

create distance as a global variable, set it when in checkRunIn, then access it when you need it in getFare

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.