romeldev/php-fhir-r5

HL7 FHIR R5 PHP library, PHP 7.4+ compatible. Generated from dcarbone/php-fhir and downgraded with Rector.

Maintainers

Package info

github.com/romeldev/php-fhir-r5

pkg:composer/romeldev/php-fhir-r5

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-04-27 14:53 UTC

This package is auto-updated.

Last update: 2026-04-27 14:57:50 UTC


README

HL7 FHIR R5 PHP library, compatible with PHP 7.4+.

This package is a downgraded build of dcarbone/php-fhir (which targets PHP 8.1+), produced with Rector so it can be consumed by legacy applications — typically Laravel projects still on PHP 7.4.

It provides fully-typed PHP models for every FHIR R5 resource and data type, JSON & XML (de)serialization, validation rules, and a ready-to-use FHIR REST client.

Requirements

  • PHP 7.4 or newer
  • PHP extensions: ctype, curl, dom, json, libxml, simplexml, xmlreader, xmlwriter

Installation

composer require romeldev/php-fhir-r5

Usage

All classes live under the Romeldev\Fhir namespace. FHIR types for the R5 version are nested under Romeldev\Fhir\Versions\R5\Types.

Parse a FHIR JSON resource

use Romeldev\Fhir\Versions\R5\Version;
use Romeldev\Fhir\Versions\R5\Types\FHIRBase\FHIRResource\FHIRDomainResource\FHIRPatient;

$version = new Version();
$json = file_get_contents('/path/to/patient.json');
$decoded = json_decode($json);

$patient = FHIRPatient::jsonUnserialize(
    $decoded,
    $version->getConfig()->getUnserializeConfig()
);

foreach ($patient->getName() as $name) {
    echo $name->getFamily()->getValue() . "\n";
}

Serialize a resource back to JSON

$encoded = json_encode($patient);

Parse FHIR XML

$xml = simplexml_load_file('/path/to/patient.xml');
$patient = FHIRPatient::xmlUnserialize(
    $xml,
    $version->getConfig()->getUnserializeConfig()
);

Use the REST client

use Romeldev\Fhir\Versions\R5\VersionClient;
use Romeldev\Fhir\Client\Client;
use Romeldev\Fhir\Encoding\SerializeFormatEnum;
use Romeldev\Fhir\Versions\R5\VersionResourceTypeEnum;

$client = new VersionClient(
    new Client('https://my-fhir-server.example.com/fhir'),
    $version
);

$response = $client->read(
    VersionResourceTypeEnum::PATIENT,
    'example-patient-id',
    SerializeFormatEnum::JSON
);

if ($response->getCode() === 200) {
    $patient = FHIRPatient::jsonUnserialize(
        json_decode($response->getResp()),
        $version->getConfig()->getUnserializeConfig()
    );
}

Namespace layout

Romeldev\Fhir\
├── Client\               → HTTP client, response objects, enums (method, sort)
├── Encoding\             → JSON/XML (un)serialization configs, XMLWriter helper
├── Types\                → Shared interfaces (ElementTypeInterface, etc.)
├── Validation\           → Validator + rule classes
└── Versions\R5\
    ├── Version              → entry point for the R5 version
    ├── VersionClient        → R5-specific REST client
    ├── VersionConfig        → config used by (un)serialize
    ├── VersionResourceTypeEnum
    └── Types\               → every FHIR R5 resource and data type

Compatibility notes

  • No PHP 8+ features at runtime. Enums, union types, match, named arguments and mixed have been transformed to their PHP 7.4 equivalents (classes with constants, PHPDoc @return, switch, positional args, untyped).
  • Constants instead of enums. What was enum Foo: string in the original is now class Foo { public const XML = 'xml'; ... }. Use Foo::XML as before — the API surface is unchanged. Type hints that originally referenced these enums (e.g. SerializeFormatEnum $format) have been rewritten to string since the constants are now string literals.
  • #[Attribute] markers on classes are preserved as #[...] lines — PHP 7.4 parses them as single-line comments, so they are inert.
  • Warnings-free under PHP 7.4 php -l (including XMLWriter subclass signature compatibility).
  • Test suite passes on PHP 7.4 — 5368 tests / 19743 assertions, using PHPUnit 9.6 and symfony/polyfill-php80 (for preg_last_error_msg()).

Running the test suite

composer install
composer test          # 5368 tests, no network required
composer test:all      # all tests, requires a FHIR R5 server at 127.0.0.1:8080/R5

Upstream & attribution

All generation logic, schemas and the original library architecture belong to Daniel Carbone and the dcarbone/php-fhir project. This package only:

  1. Generates under a different PHP namespace (Romeldev\Fhir instead of DCarbone\PHPFHIRGenerated).
  2. Transpiles the output down to PHP 7.4 with Rector.

If you can run PHP 8.1+, prefer the upstream dcarbone/php-fhir-generated directly.

License

Apache License 2.0 — same as upstream. See LICENSE.