Net_GeoIP::getInstance() – method to get an instance and avoid re-parsing the database


require_once "Net/GeoIP.php";

object getInstance() ( string $filename [, int $flags] )


This method is an implementation of the so-called singleton pattern and is the preferred way to create instances of Net_GeoIP.

Creating an instance of Net_GeoIP

require_once "Net/GeoIP.php";

$geoip Net_GeoIP::getInstance("/path/to/geoipdb.dat"Net_GeoIP::SHARED_MEMORY);

Multiple Instances

If you want to use multiple databases in one application, you will need to create an instance of Net_GeoIP for each database. Using the singleton getInstance() method will make sure that at any given point exactly one object for each database exists, which saves on overhead of setting up database segments.


  • string $filename - Name of (and path to) the database file

  • int $flags - Flags that control the class behaviour. This parameter can be one of the following class constants:

    • Net_GeoIp::SHARED_MEMORY - use SHMOP to share a database among multiple PHP instances.

      Only one Net_GeoIP instance can use shared memory at a time.

      If you are using Net_GeoIP::SHARED_MEMORY (shmop) you can only use Net_GeoIP::SHARED_MEMORY for one (1) instance (i.e. for one database). Any subsequent attempts to instantiate using SHARED_MEMORY will read the same shared memory block already initialized, and therefore will cause problems since the expected database format won't match the database in the shared memory block.

      Note that there is no easy way to flag "nice errors" to prevent attempts to create new instances using Net_GeoIP::SHARED_MEMORY flag and it is also not posible (in a safe way) to allow new instances to overwrite the shared memory block.

      In short, is you are using multiple databases, use the Net_GeoIP::SHARED_MEMORY flag with care.

    • Net_GeoIp::MEMORY_CACHE - store the full contents of the database in memory for current script.

      This is useful if you access the database several times in a script.

    • Net_GeoIp::STANDARD - standard no-cache version. This is also the default value if this parameter is ommitted.

Net_GeoIP (Previous) returns full country name for specified IP address (Next)
Last updated: Thu, 23 Feb 2017 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.