1

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!

5
  • remove the backslash before df_library? i.e. echo df_library\getUserTime($server_geoip_record)->format('Y-m-d H:i:s'); Commented May 12, 2014 at 10:20
  • Just tried that, error remains the same. Plus, considering the namespace is under "root", why would having (or not) a backslash make any difference? Commented May 12, 2014 at 10:32
  • Oh, I finally notice the issue cause: you have an extra ; after getLanguageCodeFromIP Commented May 12, 2014 at 10:42
  • 1
    That fixed it! Thank you!! Now, how do I select your answer since you put it in a comment? Also, I don't understand why what should equal to a "no-op" (an extra semicolon) results in such a drastic outcome. Commented May 12, 2014 at 11:13
  • added an answer. good to hear that you solved the problem. Commented May 12, 2014 at 11:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Proof that having more than two eyes helps at fixing issues! May I ask why a simple no-op semicolon would cause such a mess? Shouldn't PHP just pass over it?
PHP should add a warning message of potentially hidden function.
I have put error_reporting(E_ALL);, I don't get any warning.

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.