dcarbone / php-fhir-generated
Pre-generated classes from dcarbone/php-fhir
Requires
- php: ^8.1
- ext-curl: *
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- ext-xmlreader: *
- ext-xmlwriter: *
Requires (Dev)
- phpunit/phpunit: ^10.5.62 || ^11.5.50 || ^12.5.23 || ^13.1.7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v4.3.1
- v4.3.0
- v4.2.3
- v4.2.2
- v4.2.0
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v4.0.0-rc6
- v4.0.0-rc5
- v4.0.0-rc4
- v4.0.0-rc3
- v4.0.0-rc2
- v4.0.0-rc1
- v3.x-dev
- v3.2.0
- v3.1.0
- v3.0.6
- v3.0.4.1
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v2.x-dev
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-DSTU2
- dev-DSTU1
- dev-STU3
This package is auto-updated.
Last update: 2026-11-22 23:18:34 UTC
README
Pre-generated PHP classes for HL7 FHIR resources, produced by dcarbone/php-fhir.
This library provides strongly-typed PHP models for every FHIR resource, element, and primitive across multiple FHIR versions — complete with JSON/XML serialization, field validation, and a built-in REST client.
Features
- Six FHIR releases — DSTU1, DSTU2, STU3, R4, R4B, and R5
- Strongly-typed models — every resource, element, and primitive as a PHP class with full constructor support
- JSON & XML serialization — encode/decode resources using native PHP extensions
- Built-in FHIR REST client — read, create, update, patch, and delete resources via cURL
- Automatic resource parsing — detect and unserialize arbitrary FHIR responses by
resourceTypeor XML element name - Schema-driven validation — built-in rules derived from the FHIR schema, with support for custom rules
- Generated profiles (R4) — US Core, SDC, SMART App Launch, and more as subclasses of their base types, with optional automatic resolution from
meta.profile - Cross-version translation (beta) — translate a typed resource to another FHIR release and inspect notices for dropped fields
- PHP 8.1+ named arguments — clean, readable type construction without positional
nullpadding
Supported FHIR Versions
| Version | Generated from |
|---|---|
| DSTU1 | v0.0.82 |
| DSTU2 | v1.0.2 |
| STU3 | v3.0.1 |
| R4 | v4.0.1 |
| R4B | v4.3.0 |
| R5 | v5.0.0 |
Requirements
- PHP 8.1 or newer
- Extensions:
curl,dom,json,libxml,simplexml,xmlreader,xmlwriter
Installation
Via Composer (recommended)
composer require dcarbone/php-fhir-generated
Standalone
Clone or download this repository and require the built-in autoloader:
require __DIR__ . '/src/DCarbone/PHPFHIRGenerated/Autoloader.php';
Quick Start
Constructing a Resource
<?php use DCarbone\PHPFHIRGenerated\Versions\R4\Types\FHIRElement\FHIRHumanName; use DCarbone\PHPFHIRGenerated\Versions\R4\Types\FHIRResource\FHIRDomainResource\FHIRPatient; $patient = new FHIRPatient( id: 'patient-1', language: 'en-us', name: [ new FHIRHumanName(given: ['Real'], family: 'Human'), ], );
Serializing to JSON
echo json_encode($patient, JSON_PRETTY_PRINT);
Unserializing from JSON
$patient = FHIRPatient::jsonUnserialize('{"resourceType":"Patient","id":"patient-1"}');
Translating a Resource Between FHIR Versions (Beta)
With package v4.3.1 or later, pass the R4 $patient from above and the desired target version to Translator:
use DCarbone\PHPFHIRGenerated\Translation\Translator; use DCarbone\PHPFHIRGenerated\Versions\R5\Version as R5Version; $result = (new Translator())->translate($patient, new R5Version()); $r5Patient = $result->getResource(); // An R5 FHIRPatient $notices = $result->getNotices();
Translation copies compatible fields; it does not guarantee equivalent clinical meaning. The default policy tries to preserve unmappable fields as official cross-version extensions. v4.3.1 ships no extension definitions or terminology maps for its translation routes, so an unmappable field throws by default. See the worked example before opting to drop data.
Fetching from a FHIR Server
<?php use DCarbone\PHPFHIRGenerated\Client\Client; use DCarbone\PHPFHIRGenerated\Client\Config; use DCarbone\PHPFHIRGenerated\Encoding\SerializeFormatEnum; use DCarbone\PHPFHIRGenerated\Versions\R4\Version; use DCarbone\PHPFHIRGenerated\Versions\R4\VersionClient; $config = new Config( address: 'https://hapi.fhir.org/baseR4', defaultFormat: SerializeFormatEnum::JSON, ); $client = new VersionClient(new Client($config), new Version()); $patient = $client->readOnePatient(resourceID: 'example');
Validating a Resource
$errors = $patient->_getValidationErrors(); if ([] === $errors) { echo "Resource is valid!\n"; } else { foreach ($errors as $path => $fieldErrors) { foreach ($fieldErrors as $rule => $message) { echo "{$path}: {$message}\n"; } } }
Documentation
Full documentation is available on the Wiki:
- Configuration — Version, serialization, and unserialization options
- Constructing Types — Creating resource, element, and primitive instances
- Serialization — Encoding and decoding resources in JSON and XML
- FHIR Client — Built-in HTTP client for FHIR REST operations
- Resource Parsing — Automatically parsing arbitrary FHIR responses
- Cross-Version Translation — Routes, unmappable-field policies, notices, and beta limitations
- Validation — Built-in and custom validation rules
- Profiles — Generated profile classes, slice accessors, and automatic profile resolution
- Type System — Type hierarchy and interfaces
- Autoloading — Class loading with and without Composer
- Migrating from v2 — Breaking changes and upgrade guide from the previous major version
Related Projects
| Project | Description |
|---|---|
| dcarbone/php-fhir | The code generator that produces this library from FHIR schema definitions |
| HL7 FHIR | The FHIR specification itself |