可以调用两次功能吗?

时间:2016-07-08 13:38:40

标签: php

每当我运行此代码时,我都会得到:

  

PHP致命错误:无法重新声明compare_distance()

临时解决方案似乎是重复findNearestLocation()并为其指定了不同的名称,因此我没有拨打findNearestLocation()两次,而是拨打findNearestLocation1()findNearestLocation2()

我已尝试在foreach循环中取消设置变量,但我无法弄清楚我缺少的是什么。

3 个答案:

答案 0 :(得分:2)

当然,男人)

变化:

function findNearestLocation($clientLocation) {

    global $depotLocations;
    $findDepot = $depotLocations;
    $count = 0;

    foreach ($findDepot as $row) {

        $currentDepot = array($row['location_latitude'], $row['location_longitude']);
        $findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
        $count ++;

    }    

    function compare_distance($a, $b) {
        if ($a['distance'] == $b['distance']) return 0;
        return ($a['distance'] < $b['distance']) ? -1 : 1;
    }

    usort($findDepot, 'compare_distance');

    return $findDepot[0];

}

要:

function compare_distance($a, $b) {
    if ($a['distance'] == $b['distance']) return 0;
    return ($a['distance'] < $b['distance']) ? -1 : 1;
}

function findNearestLocation($clientLocation) {

    global $depotLocations;
    $findDepot = $depotLocations;
    $count = 0;

    foreach ($findDepot as $row) {

        $currentDepot = array($row['location_latitude'], $row['location_longitude']);
        $findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
        $count ++;

    }    

    usort($findDepot, compare_distance);

    return $findDepot[0];

}

或者(如果您使用的是PHP7):

function findNearestLocation($clientLocation) {

    global $depotLocations;
    $findDepot = $depotLocations;
    $count = 0;

    foreach ($findDepot as $row) {

        $currentDepot = array($row['location_latitude'], $row['location_longitude']);
        $findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
        $count ++;

    }    

    usort(
        $findDepot, 
        function ($a, $b) {return $a['distance'] <=> $b['distance'];}
    );

    return $findDepot[0];

}
祝你好运!

答案 1 :(得分:0)

事实证明,相对简单的解决方案是不在函数内部声明函数..一旦我将compare_distance()移到findNearestLocation()函数之外,它就能完美运行。

所以现在我理解了什么,但我真的不明白为什么...通过在函数内部声明一个函数来创建什么问题?

如果这似乎是一个显而易见的问题,我很抱歉,我仍然是PHP的新手。

<?php 


function findNearestLocation($clientLocation) {

global $depotLocations;
$findDepot = $depotLocations;
$count = 0;

    foreach ($findDepot as $row) {

        $currentDepot = array($row['location_latitude'], $row['location_longitude']);
        $findDepot[$count]['distance'] = distance($currentDepot,$clientLocation);
        $count ++;

    }    

    function compare_distance($a, $b) {
        if ($a['distance'] == $b['distance']) return 0;
        return ($a['distance'] < $b['distance']) ? -1 : 1;
    }

usort($findDepot, 'compare_distance1');

return $findDepot[0];

}

function distance($depot, $clientLocation) { // need to send two arrays with only longitude and latitude

list($lat1, $lon1) = $depot;
list($lat2, $lon2) = $clientLocation;

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$kilometres = $dist * 60 * 1.1515 * 1.609344;

return $kilometres;
}

// 4. ** These next two lines cause the error. If I comment out either of these lines, the code works fine and returns the expected result.

$finalPickup = findNearestLocation($pickupLocation);
$finalDropoff = findNearestLocation($dropoffLocation);

var_dump($finalPickup);
var_dump($finalDropoff);
?>

来源:@John Detlefs

答案 2 :(得分:0)

我对PHP语言的一般理解是,嗯,(耸肩),它从来就不是复杂的。它真的不知道 “嵌套函数。”它实际上只有两个变量范围。等等。

(但话又说回来,它的设计师从来没有真正开始创建“世界上最受欢迎的网络编程语言!”所以它会......)

初始编译器错误消息:cannot re-declare {foobar},是您的第一个迹象,表明该语言没有“理解”您尝试做的事情。