generate PHP classes from XSD? generate PHP classes from XSD? php php

generate PHP classes from XSD?


I'm working now on this issue and going to release the tool as soon as it reaches more-less stable state. Check here http://mikebevz.com/xsd-to-php-tool/

Upd. I've just release first working prototype, it works fine with UBL 2.0 schemas and one simple schema, but more serious testing is on the way. I'd appreciate if you send schemas you're working with, so I'd include them in the test suite.

Upd. 2. XSD2PHP reached version 0.0.5. Check the progress on https://github.com/moyarada/XSD-to-PHP


The main reasons for using XSD class generators is to

  1. Get compile time checking
  2. An easier syntax than plain old XML API's
  3. Auto completion in your IDE.

Now contrast this with PHP. PHP does not have compile time checking and it has support for dynamic methods/properties. This voids two of the main reasons above and makes this a non-issue unless you really need auto completion. In other words, there is reason to use an XSD class generator in PHP, and that is probably also why none exist.

My suggestion is to use PHPs Simple XML which creates properties to match the XML dynamically during runtime. If you validate your XML against the XSD file and then create a Simple XML object, you have your XML object structure complete with methods and properties, without having to generate code. A perfectly good approach in PHP.

Note that I don't state that SimpleXML is the same as generated XSD classes, of course not.. But it is pretty close, usage and API-wise. You still end up doing something like $company->employee[2]->firstname either way.


There is another recent tool called PiBX a JiBX inspired tool.

From the site:

PiBX is an XML-Data-Binding framework for PHP.

With PiBX you can generate PHP classes based off an available XML-Schema. These classes can be used to marshal the informations to XML without hassling with schema checks, constraints or restrictions.