Table of Contents

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;

<?php
class Senior {
 
}
 
class Junior extends Senior {
 
}
?>

In UML terms this is;

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;

<?php
class Dao {
    function getSomething() {
 
    }
}
 
class Model {
    var $dao;
    function Model (& $dao) {
        $this->dao=& $dao;
    }
 
    function doSomething () {
        $this->dao->getSomething();
    }
}
 
$dao=new Dao;
 
$model=new Model($dao);
$model->doSomething();
?>

In UML that’s

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;

<?php
class LinkWidget {
 
}
 
class View {
    var $linkWidget;
    var $page;
    function View () {
        $this->linkWidget=new LinkWidget;
    }
 
    function renderPage () {
        $this->page=$this->linkWidget->display()
    }
}
?>

In UML this is represented by;

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 :: operator is used. For example;

<?php
class HtmlUtils {
    function unHtmlEntities ($str) {
        $trans_tbl = get_html_translation_table (HTML_ENTITIES);
        $trans_tbl = array_flip ($trans_tbl);
        return strtr ($str, $trans_tbl);
    }
}
 
class View {
    function renderPage {
        $text=HtmlUtils::unHtmlEntities($text);
    }
}
?>

This would be represented as;

The message is sent from View to HtmlUtils.

Also messages could be sent both ways, such as;

<?php
class Debug {
    function display () {
        echo ($this->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” ]

Here SomeClass sends a message to Debug, which in turn accesses SomeClasses $errorMsg property.

Resources

Introduction to UML from the Object Management Group Posideon UML - a tool for drawing UML diagrams and generating Java (sadly no PHP), the community edition being free to use. Based on Argo UML, an open source project. Object Mentor on UML A UML Reference Card