Tutorials

OO PHP Part 3: UML, Classes and Relations

by John Kleijn on Jun 21, 2008 7:09:14 AM

2. Class Diagram Entities

Quick overview of how the entities in class diagrams are presented.

2.1 Classes

The visual representation of a class is simple: it’s a rectangle box with three possible compartments.

  • The top for the name of the class and optionally its stereotype (see section stereotypes)
  • The middle compartment is a list of the class properties, referred to as attributes
  • The bottom compartment is for the operations, in most applications of UML equal to methods in actual code. If an operation is underlined, that means it is a static operation

Presentation of abstract classes is identical, with the exception of the name of the class, which should be displayed italic.


2.2 Interfaces

Interfaces are very similar in presentation to classes. In fact, in terms of UML, an interface is a class. Interfaces don’t have properties or attributes, so the entity has only two compartments. In addition the stereotype <<interface>> is added, so no confusion with a class without attributes is possible.

There is an alternate notation that uses circles (also referred to as ‘lollipop’ notation), but it is unsupported by many UML editors. The lollipop notation doesn’t specify operations, and is only used for Object Oriented Analysis.

2.3 Packages

A package is an entity encapsulating and grouping other entities like classes and interfaces. PHP has no package scope, yet classes are commonly organized in fashion that give indication of packages. In terms of UML, packages look like this:

2.4 Data types

Data types are entities too. The previous entitles can also be referred to as elements, data types can not: data types don’t have a single representation in a class diagram. Instead they are referenced with the specification of an attribute or in the calling routine (applying to arguments) or return value of an operation.

Above indicates attribute someAttribute to be of type string, as well as the argument someArgument in the calling routine of someOperation. Operation someOperation is specified to return a value of data type Boolean.

Data types represent entities native to a certain programming language, not all languages support the same set of data types, and when they do their implementations can vary. UML is meant to be extensible; you may define your own data types within UML.

2.5 Entity visibility

UML class diagrams allow you to specify the visibility of entities.

MarkVisibility type
+Public
#Protected
-Private
~Package

This can be compared with visibility as definable in PHP 5, with the addition of the ‘package’ visibility type, which limits the visibility of a class member to the package it is part of. PHP knows no ‘package scope’.

Visibility is not limited to class members. Packages can have visibility too. At times, when you need to look at ‘the bigger picture’ you may want to create a package diagram. These won’t be covered in this article though.

Comments

For starters, I absolute love these tutorials. They go beyond just OO syntax to best practices and application design.
On a different note, you mentioned you would provide a list of free UML editor, but I haven't found this list.

1. judahtanthony on Jun 27, 2008 1:23:04 PM

Thanks. I forgot about that. Will rectify, later.

2. John Kleijn on Jun 28, 2008 11:50:41 AM

This is nice work. As a newcomer to PHP, this is easy for me to assimilate. Thanks.

3. loydster on Jun 30, 2008 2:04:53 PM

I'm glad you guys like it. I will be doing one more about Design Patterns before taking a break to work on a different project, after launch of that I will continue with part 5+ :)

4. John Kleijn on Jun 30, 2008 3:57:32 PM

Very well explained. Hope u'll write on the Observer pattern. How can we use that kind of pattern in php ?

5. abouchoud on Aug 21, 2008 8:56:38 AM
Login or register to post a comment.