UML (Unified Modelling Language) is a mechanism for representing software in the form of diagrams. Essentially it allows to design our applications by drawing, and if the right tools existed, even generate code from the diagrams. In this article we'll look at how PHP code is represented in UML terms, via use of the the UML class diagram. We'll jump straight in and assume knowledge of UML and concentrate on some specific PHP code and it's equivalent UML representation - this isn't meant to be a complete analysis of class diagrams. There's a collection of resources at the end to get you started, if you haven't encountered UML before. ======Inheritance====== The extends keyword in PHP allows one class to inherit from another; In UML terms this is; {{design:Inheritance.gif?102x179}} Notice the triangle touches the parent class. ======Associations====== Associations occur between classes that are not related, but may need access to each other, such as a Model and a View, the View needing the data provided by the Model to rendering the presentation. There are different types of association; =====Aggregation===== Aggregation is when one class has access to another, the second class perhaps having been instantiated outside the first. If the first object "dies", the second will continue to "live". This is commonly seen with data access objects, which may be passed to many other objects, which themselves may "die" leaving the data access object to continue. The way that's normally explained is the first class controls //part// of the second class. For example; dao=& $dao; } function doSomething () { $this->dao->getSomething(); } } $dao=new Dao; $model=new Model($dao); $model->doSomething(); ?> In UML that's {{design:Aggregation.gif?105x179}} The clear diamond touching the class with control. =====Composition===== Composition happens when one class instantiates another, the second class "dieing" when the first class dies. In other words the first class controls the //whole// of the second class. An example in PHP; linkWidget=new LinkWidget; } function renderPage () { $this->page=$this->linkWidget->display() } } ?> In UML this is represented by; {{design:Composition.gif?105x195}} A solid diamond touching the class with control. =====Messages===== Messages occur where one class "communicates" with other with out controlling it's instance. These relationship between the classes is also association. In PHP that commonly occurs where the [[http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php|::]] operator is used. For example; This would be represented as; {{design:OneWayMessage.gif?105x179}} The message is sent from View to HtmlUtils. Also messages could be sent both ways, such as; errorMsg); } } class SomeClass { var $errorMsg='This is an error message'; function someFunction () { if ( DEBUG == 1 ) { Debug::display(); } } } define ('DEBUG',1); $someClass= &new SomeClass; $someClass->someFunction(); ?> [ Outputs: "This is an error message" ] {{design:TwoWayMessages.gif?105x179}} Here SomeClass sends a message to Debug, which in turn accesses SomeClasses $errorMsg property. ======Resources====== [[http://www.omg.org/gettingstarted/what_is_uml.htm|Introduction to UML]] from the [[http://www.omg.org|Object Management Group]] [[http://www.gentleware.com/|Posideon UML]] - a tool for drawing UML diagrams and generating Java (sadly no PHP), the community edition being free to use. Based on [[http://argouml.tigris.org/|Argo UML]], an open source project. [[http://www.objectmentor.com/resources/listArticles?key=topic&topic=UML|Object Mentor on UML]] [[http://www.holub.com/class/uml/uml.html|A UML Reference Card]]