Search by

dcarbone / php-fhir-generated

dcarbone

Pre-generated classes from dcarbone/php-fhir

Package info

github.com/dcarbone/php-fhir-generated

pkg:composer/dcarbone/php-fhir-generated

Fund package maintenance!

dcarbone

Ko Fi

Statistics

Installs: 666 549

Dependents: 1

Suggesters: 0

Stars: 48

Open Issues: 1

v4.3.1 2026-11-22 04:53 UTC

README

License PHP Version

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 resourceType or 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 null padding

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

License

Apache-2.0