Jump to main content Jump to doc navigation

modX::getService

Deprecated in MODX 3.x. xPDO marks getService() as deprecated and points you at the service / DI container ($modx->services). PhpStorm therefore warns that it will be removed in 3.1. That removal did not happen. The method still exists on current 3.x, and the core still calls it. Prefer $modx->services in new code. See Dependency Injection Container.

getService lives on xPDO (modX extends xPDO). It loads and returns a named service instance, or null if it cannot load the class. The instance is created once. Later calls return the stored object. Internally 3.x stores that object in $modx->services as well.

Syntax

object getService (string $name, [string $class = ''], [string $path = ''], [array $params = array ()])
  • $name (string) key that identifies the service.
  • $class (string) class name for new, or dot notation for subfolders under $path.
  • $path (string) directory that contains the class file.
  • $params (array) second constructor argument. The first argument is always the xPDO/MODX instance.

Defined in xPDO::getService().

Examples (still work)

Get the Smarty service:

$modx->getService('smarty','smarty.modSmarty');

Custom service with a path and constructor params:

$modx->getService('twitter','modTwitter','/path/to/',array(
  'api_key' => 3212423,
));
$modx->twitter->tweet('Success!');

Inside an Extra:

// Path points at the class directory:
if(!$Product = $this->modx->getService('mypkg.product','Product',MODX_CORE_PATH.'components/mypkg/model/mypkg/')) {
    return 'NOT FOUND';
}
// Or use dot notation and point $path at the model directory:
if(!$Product = $this->modx->getService('mypkg.product','mypkg.Product',MODX_CORE_PATH.'components/mypkg/model/')) {
    return 'NOT FOUND';
}

getService can struggle with PHP namespaces. Pass a fully qualified class name, or register the object on the container yourself.

Replacement in MODX 3

Use $modx->services (has / add / get). Core does the same in modX::runProcessor() for lexicon and error.

Register a core-style service (example: modError):

use MODX\Revolution\Error\modError;

if (!$modx->services->has('error')) {
    $modx->services->add('error', new modError($modx));
}
$modx->error = $modx->services->get('error');

Custom Extra: instantiate the class (Composer / namespace autoload) and add it. A Namespace bootstrap.php is the usual place:

$modx->services->add('twitter', function($c) use ($modx) {
    return new MyPackage\Twitter($modx, ['api_key' => 3212423]);
});

Then:

$twitter = $modx->services->get('twitter');
$twitter->tweet('Success!');

modError ($modx->error) and modErrorHandler ($modx->errorHandler) are different services. Do not mix the keys.

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

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

Learn more