Jump to main content Jump to doc navigation

modX::getInstance

Create, retrieve or update modX instances. This is the standard way to get a modX object: every MODX entry point uses it and registers the result in the shared instance registry.

Syntax

static modX getInstance ([string|int|null $id = null], [array|null $config = null], [boolean $forceNew = false])
  • $id (string|int|null) Identifier of the instance. When omitted, the existing instance is returned if there is one; otherwise a new id is generated and a new instance is created.
  • $config (array|null) Config data for the instance. Passing it with an $id that already exists merges the values into that instance's config; passing it without an $id forces the creation of a new instance.
  • $forceNew (boolean) Force the creation of a new instance even if one with the given $id already exists.

Returns the modX instance; throws xPDOException if the registry entry is not a valid instance.

Registering matters: an object created with new \MODX\Revolution\modX() runs, but it is not stored in the registry. The next getInstance() call — from an entry point, setup or a test — then builds a second instance with its own database connection and config. Use getInstance() everywhere.

Example

Load MODX externally through the instance registry:

require_once '/absolute/path/to/modx/config.core.php';
require_once MODX_CORE_PATH . 'vendor/autoload.php';

$modx = \MODX\Revolution\modX::getInstance();
$modx->initialize('web');

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$194 per month—let's make that $500!

Learn more