質問

I have slapped together a test PHP script. It would output some remote connection's geo ip based data. Nothing fancy, just a quick prototype.

But I am seeing an odd behavior, so I am asking here if somebody had any clues about it. PHP is version 5.5.12 on Ubuntu 64 bit.

Here's some code from the geoip_test.php calling script:

require_once ('geoip_utils.php');

$server_geoip_record = geoip_record_by_name('php.net');
echo '<pre>PHP.net web server location: ' . print_r($server_geoip_record, 1);
echo '<br />PHP.net web server local time: ' . \df_library\getUserTime($server_geoip_record)->format('Y-m-d H:i:s');

Nothing fancy at all, isn't it?

Now the simple geoip_utils.php code:

<?php

namespace df_library;

require_once('timezone.php');

// Given an IP address, returns the language code (i.e. en)
function getLanguageCodeFromIP($input_ip)
{

};

// Given a geo_ip_record, it returns the local time for the location indicated
// by it. In case of errors, it will return the optionally provided fall back value
function getUserTime($geoip_record, $fall_back_time_zone = 'America/Los_Angeles') {
    //Calculate the timezone and local time
    try
    {
        //Create timezone
        $timezone = @get_time_zone($geoip_record['country_code'], ($geoip_record['region'] != '') ? $geoip_record['region'] : 0);

        if (!isset($timezone)) {
            $timezone = $fall_back_time_zone;
        }

        $user_timezone = new \DateTimeZone($timezone);

        //Create local time
        $user_localtime = new \DateTime("now", $user_timezone);       
    }
    //Timezone and/or local time detection failed
    catch(Exception $e)
    {
        $user_localtime = new \DateTime("now");
    }

    return $user_localtime;
}

?>

When I run the calling script I get:

PHP Fatal error:  Call to undefined function df_library\getUserTime() in /var/www/apps/res/geoip_test.php on line 5

The funny part is: if I add this debug code:

$x = get_defined_functions();
print_r($x["user"]);

I get this output:

Array
(
    [0] => df_library\getlanguagecodefromip
    [1] => df_library\gettimezone
    [2] => df_library\getutcdatetime
    [3] => df_library\getlocalizedtime
    [4] => df_library\getutcdatetimeaslocalizeddatetime
    [5] => df_library\getlocalizeddatetimeasutcdatetime
    [6] => get_time_zone
)

First of all, I don't understand why the function names are converted to lower case. But most of all, notice how index 0 shows the empty function function getLanguageCodeFromIP($input_ip) being defined, and that function is right above the one that the interpreter complains about as not being defined! Why does PHP see the other function in that file but not the one I want to use?

Any ideas are welcome!

役に立ちましたか?

解決

There is an extra semi-colon ; after the close bracket of function getLanguageCodeFromIP which causes PHP parser somehow unable to recognize the functions after getLanguageCodeFromIP.

As proven in OP's comment, removing the ; solved the problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top