【问题标题】:Convert anonymous PHP function to work with PHP < 5.3 [duplicate]将匿名 PHP 函数转换为使用 PHP < 5.3 [重复]
【发布时间】:2013-03-29 20:08:39
【问题描述】:

我很难将这个 PHP 5.3 引入的匿名函数转换为与早期版本的 PHP 一起使用。

我得到的错误是: 意外的 T_FUNCTION 和 $testResult 上的内容。这是匿名函数。任何人都可以将其转换为使用早期版本的 PHP。代码在一个类中。

代码如下:

abstract class geocoder {
    public $ch = null;
    public $xd = null;
    public $xp = null;

    public function __construct() {
        $this->ch = curl_init();
        curl_setopt_array($this->ch, array (
            CURLOPT_BINARYTRANSFER => true,
            #CURLOPT_FAILONERROR => true,
            CURLOPT_FOLLOWLOCATION => false,
            #CURLOPT_FORBID_REUSE => true,
            #CURLOPT_MUTE => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 2,
            #CURLOPT_COOKIEJAR => '/dev/null',
            #CURLOPT_COOKIEFILE => '/dev/null',
            #CURLOPT_INTERFACE => 'x.x.x.x',
            CURLOPT_USERAGENT => 'curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5',
            CURLOPT_HEADER => false));
    }

    public function doreq($u) {
        curl_setopt($this->ch, CURLOPT_URL, $u);
        return curl_exec($this->ch);
    }

    abstract public function dogeocode($q);
}

class geocodeGMaps extends geocoder {
    public function __construct() {
        parent::__construct();
        #$this->xd = new DOMDocument();
        #$this->xp = new DOMXpath($this->xd);
    }

    public function testResult(&$res) {
            switch (true) {
                case !$res:
                    return 'Remote call HTTP request failed';
                case !($res = @json_decode($res, true)):
                    return 'Failed to parse JSON result';
                case !isset($res['status']):
                    return 'Remote call returned NULL status';
                case !($res['status'] == 'OK'):
                    return 'Remote call returned !OK status';
                case !isset($res['results']):
                case !isset($res['results'][0]):
                    return 'NULL or empty result set';
                case !isset($res['results'][0]['geometry']['location_type']):
                    return 'NULL location type';
                case !($res['results'][0]['geometry']['location_type'] == 'ROOFTOP'):
                    return '';
                case !isset($res['results'][0]['geometry']['location']):
                case !isset($res['results'][0]['geometry']['location']['lat']):
                case !isset($res['results'][0]['geometry']['location']['lng']):
                    return 'No location returned in result';
                default:
                    return NULL;
                    break;
            }
            return NULL;
    }

    public function dogeocode($q) {
        $res = @$this->doreq("http://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($q) . "&sensor=false");

        if ($testResult = testResult($res))
            throw new Exception ($testResult);

        $comp = array('query' => &$q);
        $map = array(
            'street_number' => 'street_number',
            'route' => 'route',
            'city' => 'locality', 
            'county' => 'administrative_area_level_2',
            'state' => 'administrative_area_level_1',
            'country' => 'country',
            'zip' => 'postal_code');

        if (isset($res['results'][0]['address_components'])) {
            foreach ($res['results'][0]['address_components'] as &$v)
                foreach ($map as $k => &$l)
                    if (in_array($l, $v['types']) && !isset($comp[$k]))
                        $comp[$k] = $v['long_name'];
        }

        return array ('formatted' => (isset($res['results'][0]['formatted_address']) ? $res['results'][0]['formatted_address'] : NULL),
                  'components' => &$comp,
                  'latlng' => array ($res['results'][0]['geometry']['location']['lat'],
                              $res['results'][0]['geometry']['location']['lng']));
    }
}

谁能帮助在 PHP 5.2 中完成这项工作?

【问题讨论】:

  • 什么是早期版本不能正常工作?

标签: php


【解决方案1】:

做事

function testResult(&$res) {

    // ... code

}

然后:

if ($testResult = testResult($res))
    throw new Exception ($testResult);

编辑:

现在我可以看到这是在一个类中,您需要将新的testResult 函数从dogeocode 中移出,并使其成为防止Fatal error: Cannot redeclare testResult() 的新方法。然后,您将使用以下方式访问它:

if ($restResult = $this->testResult($res))
    throw new Exception ($testResult);

【讨论】:

  • 我试过了,但没用。实际上这个函数在一个类里面。我尝试了所有可能的方法来完成这项工作,但没有成功。
  • 不以什么方式工作?有错误吗?出乎意料的结果?
  • 我现在已经更新了代码。希望这有助于理解代码试图做什么。
  • 你没有做出我建议的改变。
  • 您建议的更改不会引发错误,但不会按照预期进行。
猜你喜欢
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多