This package is abandoned and no longer maintained. The author suggests using the php-ffi/ide-helper-generator package instead.

CastXml wrapper for PHP

dev-master / 1.0.x-dev 2023-08-10 23:55 UTC

This package is auto-updated.

Last update: 2023-08-10 23:56:32 UTC


!!! This package is deprecated, please use instead

CastXML PHP Wrapper

CastXML is a C-family abstract syntax tree XML output tool.

This project is maintained by Kitware in support of ITK, the Insight Segmentation and Registration Toolkit.


Library can be installed into any PHP application using Composer dependency manager.

  • composer require serafim/castxml

In order to access library make sure to include vendor/autoload.php in your file.


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


Don't forget to install the binary dependency. If you are looking for pre-built binaries, or a compact way to build this project, please see CastXMLSuperbuild.

Also, some operating systems support installation using dependency managers, for example:

$ sudo apt update
$ apt install castxml


To create a new CastXML, you can use one of the possible options.

$castxml = new \Serafim\CastXml\CastXml();

// OR

$binary = '/path/to/binary/castxml';
$castxml = new \Serafim\CastXml\CastXml($binary);

Windows OS is also supported, just write the path to CastXML in PATH variable or add the path to castxml.exe to the class constructor.

Availability Check

For information on whether everything is OK, simply use the accessibility isAvailable() method.

$castxml = new \Serafim\CastXml\CastXml();

$available = $castxml->isAvailable();

// - bool(true)  - CastXML is available
// - bool(false) - CastXML is not available


For version information use methods getVersion() and getClangVersion().

$castxml = new \Serafim\CastXml\CastXml();

echo $castxml->getVersion();
// For example: "0.4.2"

echo $castxml->getClangVersion();
// For example: "11.0.0"


To parse the original header, just use the parse() method. The method takes one required $pathname and one optional $cwd string arguments.

$castxml = new \Serafim\CastXml\CastXml();

$result = $castxml->parse('/path/to/header.h');
// OR
$result = $castxml->parse('/path/to/header.h', '/path/to/working_directory');

As a result, you will get a Serafim\CastXml\Result object.

$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');

echo $result->getContents(); // XML output
// OR
echo $result; // Same XML output

Result Saving

$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');

// Save to file

// Save into directory

Result Parsing

$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');

// Using SimpleXML
//  - ext-simplexml required
$simplexml = $result->toXml();

// Using XML Reader
//  - ext-xmlreader required
$reader = $result->toXmlReader();

// Using DOMDocument
//  - ext-dom required
$ast = $result->toDomDocument();

Result PHP Types

$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');

foreach ($result as $type) {