One of the most useful tools for modeling object-oriented programs is UML, Unified Modeling Language. This chapter introduces you to UML and discusses why you should use it, what software is available to PHP programmers to create UML, and how the various UML diagrams you create help you build your applications. The UML diagrams you produce at the start of the chapter will develop into a full example application: the contact manager.
The contact manager is intended to manage data about individuals and organizations and enable users to look up and edit the contact information about these entities. The discussion about the sample program provides a good discourse on the issues behind creating working object-oriented applications. Along the way, it also demonstrates the major principles behind the object-oriented paradigm such as code reuse, encapsulation, and of course, abstraction.
The Unified Modeling Language
With the cumbersome vocabulary of OOP, it can be difficult to get your head around a written description of a set of objects. However, carefully planning and clearly documenting your objects is crucial to the successful delivery of an application. This is especially true in a large application that's using dozens of objects. But how do you generate this documentation in a manner that will be accessible to those who need to read it? As the saying goes, a picture is worth a thousand words. The Unified Modeling Language (UML) is a specification that describes standard processes for generating a visualization of object-oriented software systems.
UML isn't a language—not in the usual sense of the word, anyway. It's a system for representing classes and the interactions between them in a visual way, using standardized symbols to build a complete graphical model of the application. You use images to clearly communicate complex class relationships and provide a document that others can use to understand your software and how to use it (or that you can use to understand your own software when you need to make changes to it long after the initial build). Prior to the publication of the UML specification, there were a number of competing symbologies for representing these concepts. The UML spec took the best features from each of these other systems and combined them into a single standard that deprecated the previous systems and established a common visual language.
The current version of the UML specification defines 12 different types of diagrams that describe the structure, behavior, or organization of an application, depending on which diagram you are creating. This chapter covers only one of those diagrams in detail, the class diagram, although you'll get a peek at a few other kinds of UML diagrams. A complete discussion of UML is beyond the scope of this book; if you want to learn more about UML, there are a number of excellent resources available, including UML 2 Toolkit, published by Wiley & Sons (ISBN: 0-471-46361-2).
Why Would You Want to Use UML?
Many software developers complain about having to write documentation. Often these documents are meant for consumption by clients, business development people, and other nontechnical staff, and creating them distracts from what you really want to be doing—writing code. Even the developer-oriented documents, such as a technical specification, often go untouched and unread after first being generated. UML diagrams are meant to be a working tool for describing your project and the act of creating them really, truly benefits the developer during the software development process, not just in the planning stages.
UML diagrams enable you to put to paper the ideas you have in your head about how to architect your application without all the time-wasting fuss of having to write text to describe your ideas. While you're still in the planning stages of a project, the diagrams make it easy for you to render large-scale changes to the software architecture without having to worry about the code. They provide a vehicle by which software architects can communicate their ideas to those who will be helping them to implement those ideas. And, most impressively, depending on the tools you're using to create your diagrams, you can actually generate much of the code for your classes from the diagrams. You can literally draw your code!
UML Software
There are dozens of different application out there that are capable of generating UML diagrams, both open-source and commercial software. Unfortunately, support for automatic code generation for PHP is fairly limited at time of writing. Here are a just a few of the tools you might want to consider:
- Dia (www.lysator.liu.se/~alla/dia/): A GPL'ed gtk+-based tool that has support for UML diagrams along with a wide variety of other diagram types. It has a precompiled binary available for Windows. Has no support for automatic code generation. All the UML diagrams in this book were generated using Dia.
- Visio (www.microsoft.com/office/visio): Part of the Microsoft Office suite of applications, it is used for everything from UML diagrams, to network diagrams, to floor plans and organization charts. Dia was meant to be the open-source alternative to Visio. Visio also has no facility for automatic code generation.
- IBM Rational Rose (www.rational.com): Long the gold standard for UML development, Rational provides not only software, but also a complete methodology for software development. Code generators are available for Java and .NET, but there's no support for PHP.
- ArgoUML (http://argouml.tigris.org): ArgoUML itself is written in Java but it currently has experimental support for PHP code generation. It is free and covered by a BSD-style license (see the ArgoUML Web site for more information).
- Umbrello (http://uml.sourceforge.net): A Unix-only (no Windows binaries) UML tool with full PHP code generation support. Open source.
- Poseidon For UML, Professional Edition (www.gentleware.com/products/): Commercial UML tool with full support for PHP code generation.
Although you won't need any of these tools to follow along with the book, it's recommended that you download one of them so that you can create UML diagrams of your own as you work through the chapter. Dia is used in this book, so you may want to download that one—simply go to the site and follow the instructions. There's very little required in the way of initialization and installation, so you should have no problems there.
There are two reasons why PHP code generation support is limited or nonexistent in most of these tools. Traditionally, software developers in scripting languages such as PHP rarely used the rigorous software development processes common to Java and C++ developers. UML is a process tool and as a result, PHP support was overlooked. The other reason is that until PHP5, there was only rudimentary OO support in PHP. Now that PHP has real OO support, I expect to see more of these UML tools providing PHP code generation facilities.
Class Diagrams
Ready to get started? As mentioned earlier, this book covers only one of the 12 diagrams defined by the UML specification in detail—the class diagram. A class diagram shows the classes in an application and the relationships among them.
The basic class symbol is a rectangle divided into three parts. The first part shows the class name, the second shows its properties, and the third shows its methods. The sections are separated by a horizontal line.
This example has a class called MyClass, with two public properties (property1, an Integer, and property2, a String) and one private member variable (privateData, an Array). There are also two methods, method1() is a public method that takes one Integer param1 as its parameter with a default value of 0 and returns a Boolean value. The private method privateMethod() takes no parameters and has no return value (Void). You can see that a plus sign next to a property or method indicates a public item, whereas a minus sign indicates a private member. Protected items are indicated by an octothorpe (#, also called the hash or pound symbol).
Obviously all of this is a lot easier to get from looking at the diagram than from reading the text description. This is why you use UML to describe your objects. It took me nearly three times as long to type up and format that description than it did to create the diagram in Dia.
Of course, if all you had was one class, the diagram isn't all that useful. It's only useful in a hierarchy of objects. The next section will walk you through the creation of a UML diagram for a real-world application that you will actually begin building here.
