[php] PHP UML Generator

How do I generate UML diagram based on existing classes in PHP?

This question is related to php uml class-diagram

The answer is


If you are looking to generate UML easily from your existing PHP Classes you might want to consider PHPStorm 3.0 IDE. It does a good job of replicating existing code into UML.

Have a look at the PHP Storm feature list.


Well to be honest, first and foremost you shouldn't generate UML model from code, but code from UML model ;).

Even if you are in a rare situation, when you need to do this reverse engineering, it is generally suggested that you do it by hand or at least tidy-up the diagrams, as auto-generated UML has really poor visual (=information) value most of the time.

If you just need to generate the diagrams, it's probably a good thing to ask yourself why exactly? Who is the intended audience and what is the goal? What does the auto-generated diagram have to offer, what code doesn't?

Basicly I accept only one answer to that question. It just got too big and incomprehensible.

Which again is a reason to start with UML in the first place, as opposed to start coding ;) It's called analysis and it's on decline, because every second guy in business thinks it's a bit too expensive and not really necessary.


Have you tried Autodia yet? Last time I tried it it wasn't perfect, but it was good enough.


If you are looking to generate UML easily from your existing PHP Classes you might want to consider PHPStorm 3.0 IDE. It does a good job of replicating existing code into UML.

Have a look at the PHP Storm feature list.


Have you tried Autodia yet? Last time I tried it it wasn't perfect, but it was good enough.


There's also the PHP UML tool available from pear.

PHP_UML:

  • Can generate UML/XMI files in version 1.4, or in version 2.1 (logical, component, and deployment views)
  • Can generate an API documentation in HTML format
  • Can generate PHP code (code skeleton) from a given XMI file
  • Can convert UML/XMI content from version 1.4 to version 2.1

Install it on the command line via:

$ pear install pear/php_uml

(This used to be $ pear install pear/php_uml-alpha but the package has since gone stable.)

Generate your xmi:

$ phpuml -o project.xmi


I strongly recommend BOUML which:

  • is extremely fast (fastest UML tool ever created, check out benchmarks),
  • has rock solid PHP import and export support (also supports C++, Java, Python)
  • is multiplatform (Linux, Windows, other OSes),
  • is full featured, impressively intensively developed (look at development history, it's hard to believe that such fast progress is possible).
  • supports plugins, has modular architecture (this allows user contributions, looks like BOUML community is forming up)

In theory you can use PhpStorm to visualise your classes using UML. The generation is not really great but you can effectively refactor stuff and again, at least preview parents, implementations, constants, attributes, methods and their visibility in a nice way.

Situation

I want to visualise a communication between already existing components to a colleague.

Process using PHPStorm

https://blog.jetbrains.com/phpstorm/2017/09/uml-diagrams-in-phpstorm-2017-2/

Advantages

  • Nice UI, final diagram.
  • Able to refactor code from a diagram.
  • Able to add notes.
  • The class diagram symbolises private/public properties, constructors, methods nicely.

Disadvantages

  • No support for PHP 7.
  • Painfully to use. Can't resize the generated boxes.
  • When adding a new relation, the previous ones get randomly lost :O wtf?
  • Restarting PhpStorm destroys the diagrams
  • Changed my mind, impossible to use relations

Result

Anyway, after some painful hour of work I was only able to generate unrelated boxes and had to use additional program to link relations. Really bad. But I believe once they make it work properly it will be a great feature because as the code changes, the diagrams would be automatically updated!

For now, don't use PhpStorm for UML diagrams.


You can use Visual Paradigm for UML. This might not be the best paid (it's US$699) product, just as an option if anyone would like to try. It can create class diagram from PHP and vice versa, and not only PHP, there's a bunch of language you can choose such as C#, C++, Ruby, Java, VB.NET, Python, Objective C, Perl, etc. There's also a trial you can check on.


Have you tried Autodia yet? Last time I tried it it wasn't perfect, but it was good enough.


In theory you can use PhpStorm to visualise your classes using UML. The generation is not really great but you can effectively refactor stuff and again, at least preview parents, implementations, constants, attributes, methods and their visibility in a nice way.

Situation

I want to visualise a communication between already existing components to a colleague.

Process using PHPStorm

https://blog.jetbrains.com/phpstorm/2017/09/uml-diagrams-in-phpstorm-2017-2/

Advantages

  • Nice UI, final diagram.
  • Able to refactor code from a diagram.
  • Able to add notes.
  • The class diagram symbolises private/public properties, constructors, methods nicely.

Disadvantages

  • No support for PHP 7.
  • Painfully to use. Can't resize the generated boxes.
  • When adding a new relation, the previous ones get randomly lost :O wtf?
  • Restarting PhpStorm destroys the diagrams
  • Changed my mind, impossible to use relations

Result

Anyway, after some painful hour of work I was only able to generate unrelated boxes and had to use additional program to link relations. Really bad. But I believe once they make it work properly it will be a great feature because as the code changes, the diagrams would be automatically updated!

For now, don't use PhpStorm for UML diagrams.


the best (Windows) software i have found to do PHP and UML is Sparx Systems Enterprise Architect. besides a pletora of features, it supports the following for PHP:

  • Reverse engineer object oriented PHP into UML class diagrams
  • Generate PHP class definitions from UML class diagrams
  • Synchronize changes made in a UML class into the corresponding PHP class definition
  • Synchronize changes made in a PHP class definition into the corresponding UML class
  • Create UML sequence diagrams to show what PHP classes use and how they are used
  • Produce detailed documentation of your PHP code in standard RTF and HTML format
  • Perform code engineering on models to generate base PHP pages.

not free ($199), but definitely worth the money.


There's also php2xmi. You have to do a bit of manual work, but it generates all the classes, so all you have to do is to drag them into a classdiagram in Umbrello.

Otherwise, generating a diagram with the use of reflection and graphviz, is fairly simple. I have a snippet over here, that you can use as a starting point.


Here's how I did it (directly from code to PDF drawing without manual drawing of anything):

  1. Use BOUML for "reverse engineering PHP code" [sic] to extract the class model (BOUML is available from "universe" repository of Ubuntu). I seriously recommend BOUML for this step because it's really fast compared to many other programs I have tried. In addition, it seems that BOUML seems to extract the model correctly (for the parts that BOUML even tries to extract).
  2. Use BOUML to export model as XMI 1.4 file
  3. Use ArgoUML to import said XMI file (you can use webstart version for this step)
  4. Export XMI from ArgoUML (I don't know which XMI version/variant the output is but it is not the same result as the output from BOUML. The argouml-graphviz cannot handle XMI file directly from BOUML).
  5. Use argouml-graphviz to convert ArgoUML exported XMI file to dot format (you may need to use saxon instead of xsltproc to get it work due to use of XSLT2)
  6. Use dot or fdp or sfdp to render the class diagram.

Here's an example of suitable command line for using fdp to output PDF diagram (assuming that dot file generated by argouml-graphviz XLST processing is saved as xmi-model.dot):

fdp -Tpdf -Gmaxiter=1000 -Gmindist=0.5 -Gpackmode=node \
  -Eweight=0.05 -Elen=1.0 -Eminlen=1.0 -Gsplines=true \
  -Goverlap=false xmi-model.dot -oxmi-model.pdf

As an alternative you could try PHP_UML or php2xmi instead of BOUML for doing the "reverse engineering" part. I haven't yet tried that.

(I'm using the phrase "reverse engineering" because it seems that UML people are using those words when they mean extracting class and method information from the source code. I would personally interpret those words as extracting information from executable binary file or captured raw wire data.)

If you prefer drawing the class diagram by hand (instead of using computer to do all the drawing), you can use either BOUML or ArgoUML for the drawing. Using the "reverse engineered" data via BOUML will help in that case.


You can use Visual Paradigm for UML. This might not be the best paid (it's US$699) product, just as an option if anyone would like to try. It can create class diagram from PHP and vice versa, and not only PHP, there's a bunch of language you can choose such as C#, C++, Ruby, Java, VB.NET, Python, Objective C, Perl, etc. There's also a trial you can check on.


There's also the PHP UML tool available from pear.

PHP_UML:

  • Can generate UML/XMI files in version 1.4, or in version 2.1 (logical, component, and deployment views)
  • Can generate an API documentation in HTML format
  • Can generate PHP code (code skeleton) from a given XMI file
  • Can convert UML/XMI content from version 1.4 to version 2.1

Install it on the command line via:

$ pear install pear/php_uml

(This used to be $ pear install pear/php_uml-alpha but the package has since gone stable.)

Generate your xmi:

$ phpuml -o project.xmi


phUML

phUML is fully automatic UML class diagramm generator written in PHP, licensed under the BSD license. It is capable of parsing any PHP5 object oriented source code and create an appropriate image representation of the oo structure based on the UML specification.

UML Example

./phuml -r /var/www/my_project -graphviz -createAssociations false -neato out.png

Step by step guide


I strongly recommend BOUML which:

  • is extremely fast (fastest UML tool ever created, check out benchmarks),
  • has rock solid PHP import and export support (also supports C++, Java, Python)
  • is multiplatform (Linux, Windows, other OSes),
  • is full featured, impressively intensively developed (look at development history, it's hard to believe that such fast progress is possible).
  • supports plugins, has modular architecture (this allows user contributions, looks like BOUML community is forming up)

There's also php2xmi. You have to do a bit of manual work, but it generates all the classes, so all you have to do is to drag them into a classdiagram in Umbrello.

Otherwise, generating a diagram with the use of reflection and graphviz, is fairly simple. I have a snippet over here, that you can use as a starting point.


Here's how I did it (directly from code to PDF drawing without manual drawing of anything):

  1. Use BOUML for "reverse engineering PHP code" [sic] to extract the class model (BOUML is available from "universe" repository of Ubuntu). I seriously recommend BOUML for this step because it's really fast compared to many other programs I have tried. In addition, it seems that BOUML seems to extract the model correctly (for the parts that BOUML even tries to extract).
  2. Use BOUML to export model as XMI 1.4 file
  3. Use ArgoUML to import said XMI file (you can use webstart version for this step)
  4. Export XMI from ArgoUML (I don't know which XMI version/variant the output is but it is not the same result as the output from BOUML. The argouml-graphviz cannot handle XMI file directly from BOUML).
  5. Use argouml-graphviz to convert ArgoUML exported XMI file to dot format (you may need to use saxon instead of xsltproc to get it work due to use of XSLT2)
  6. Use dot or fdp or sfdp to render the class diagram.

Here's an example of suitable command line for using fdp to output PDF diagram (assuming that dot file generated by argouml-graphviz XLST processing is saved as xmi-model.dot):

fdp -Tpdf -Gmaxiter=1000 -Gmindist=0.5 -Gpackmode=node \
  -Eweight=0.05 -Elen=1.0 -Eminlen=1.0 -Gsplines=true \
  -Goverlap=false xmi-model.dot -oxmi-model.pdf

As an alternative you could try PHP_UML or php2xmi instead of BOUML for doing the "reverse engineering" part. I haven't yet tried that.

(I'm using the phrase "reverse engineering" because it seems that UML people are using those words when they mean extracting class and method information from the source code. I would personally interpret those words as extracting information from executable binary file or captured raw wire data.)

If you prefer drawing the class diagram by hand (instead of using computer to do all the drawing), you can use either BOUML or ArgoUML for the drawing. Using the "reverse engineered" data via BOUML will help in that case.


Well to be honest, first and foremost you shouldn't generate UML model from code, but code from UML model ;).

Even if you are in a rare situation, when you need to do this reverse engineering, it is generally suggested that you do it by hand or at least tidy-up the diagrams, as auto-generated UML has really poor visual (=information) value most of the time.

If you just need to generate the diagrams, it's probably a good thing to ask yourself why exactly? Who is the intended audience and what is the goal? What does the auto-generated diagram have to offer, what code doesn't?

Basicly I accept only one answer to that question. It just got too big and incomprehensible.

Which again is a reason to start with UML in the first place, as opposed to start coding ;) It's called analysis and it's on decline, because every second guy in business thinks it's a bit too expensive and not really necessary.


the best (Windows) software i have found to do PHP and UML is Sparx Systems Enterprise Architect. besides a pletora of features, it supports the following for PHP:

  • Reverse engineer object oriented PHP into UML class diagrams
  • Generate PHP class definitions from UML class diagrams
  • Synchronize changes made in a UML class into the corresponding PHP class definition
  • Synchronize changes made in a PHP class definition into the corresponding UML class
  • Create UML sequence diagrams to show what PHP classes use and how they are used
  • Produce detailed documentation of your PHP code in standard RTF and HTML format
  • Perform code engineering on models to generate base PHP pages.

not free ($199), but definitely worth the money.


Have you tried Autodia yet? Last time I tried it it wasn't perfect, but it was good enough.


phUML

phUML is fully automatic UML class diagramm generator written in PHP, licensed under the BSD license. It is capable of parsing any PHP5 object oriented source code and create an appropriate image representation of the oo structure based on the UML specification.

UML Example

./phuml -r /var/www/my_project -graphviz -createAssociations false -neato out.png

Step by step guide


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to uml

UML diagram shapes missing on Visio 2013 Use IntelliJ to generate class diagram How to show "if" condition on a sequence diagram? Eclipse plugin for generating a class diagram Generate UML Class Diagram from Java Project How to use doxygen to create UML class diagrams from C++ source Explanation of the UML arrows What's is the difference between include and extend in use case diagram? What is the difference between aggregation, composition and dependency? What is the difference between association, aggregation and composition?

Examples related to class-diagram

How to generate Class Diagram (UML) on Android Studio (IntelliJ Idea) Use IntelliJ to generate class diagram Eclipse plugin for generating a class diagram Generate UML Class Diagram from Java Project How to use doxygen to create UML class diagrams from C++ source In UML class diagrams, what are Boundary Classes, Control Classes, and Entity Classes? PHP UML Generator