Skip to content

Autoloading

Daniel Carbone edited this page Apr 2, 2026 · 1 revision

Autoloading

This library supports two autoloading strategies: Composer's PSR-4 autoloader, and a built-in autoloader for standalone usage.

With Composer (Recommended)

When installed via Composer, the library's PSR-4 namespace mapping is registered automatically. Simply require Composer's autoloader as usual:

<?php

require __DIR__ . '/vendor/autoload.php';

No additional configuration is needed — all classes under DCarbone\PHPFHIRGenerated\ are resolved automatically.

Without Composer (Standalone)

If you are not using Composer, require the library's built-in autoloader:

<?php

require __DIR__ . '/path/to/src/DCarbone/PHPFHIRGenerated/Autoloader.php';

This immediately registers the autoloader via spl_autoload_register().

How the Built-in Autoloader Works

The Autoloader class uses a two-tier strategy:

1. Core Class Map

Shared (non-version-specific) classes — interfaces, traits, encoding, validation, and client classes — are mapped in a static class map. When one of these classes is requested, it is loaded directly from the map.

Core classes include:

  • DCarbone\PHPFHIRGenerated\Constants
  • DCarbone\PHPFHIRGenerated\FHIRVersion
  • DCarbone\PHPFHIRGenerated\Client\*
  • DCarbone\PHPFHIRGenerated\Encoding\*
  • DCarbone\PHPFHIRGenerated\Types\*
  • DCarbone\PHPFHIRGenerated\Validation\*
  • DCarbone\PHPFHIRGenerated\Versions\VersionConfig
  • DCarbone\PHPFHIRGenerated\Versions\VersionInterface
  • etc.

2. Lazy Version Autoloaders

Each FHIR version (DSTU1, DSTU2, STU3, R4, R4B, R5) has its own autoloader class. Version autoloaders are registered lazily — they are only loaded and registered when a class from that version's namespace is first requested.

This means:

  • If your application only uses R4 types, only the R4 autoloader will be activated.
  • The DSTU1, DSTU2, STU3, R4B, and R5 autoloaders will never be loaded, saving memory and startup time.

Registration

The built-in autoloader registers itself as soon as Autoloader.php is required:

// At the bottom of Autoloader.php:
Autoloader::register();

You can also manually unregister it if needed:

<?php

use DCarbone\PHPFHIRGenerated\Autoloader;

Autoloader::unregister();

Mixing Autoloaders

If you are using Composer but also want the built-in autoloader (e.g., during development), both can coexist. Composer's autoloader will typically resolve classes first. The built-in autoloader acts as a fallback.

Clone this wiki locally