UMLing your PHP project

As I don’t have any background on UML, I decided to read about it from scratch. I found at least three tutorials much better of what I could write so I’ll be quoting others

What is UML?

Umbrello docs – The Unified Modelling Language (UML) is a diagramming language or notation to specify, visualize and document models of Object Orientated software systems. UML is not a development method, that means it does not tell you what to do first and what to do next or how to design your system, but it helps you to visualize your design and communicate with others. UML is controlled by the Object Management Group (OMG) and is the industry standard for graphically describing software.

PHPKitchen – Why is UML important?

The heart of object-oriented problem solving is the construction of a model. The model abstracts the essential details of the underlying problem from its usually complicated real world. Several modeling tools are wrapped under the heading of the UML, which stands for Unified Modeling Language. The purpose of this course is to present important highlights of the UML.

At the center of the UML are its nine kinds of modeling diagrams, which we describe here.

UML and PHP

PHPPatterns – Now lets 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.

Inheritance

The extends keyword in PHP allows one class to inherit from another;

[coolcode lang=”php”]
< ?php class Senior { } class Junior extends Senior { } ?>
[/coolcode]
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;
    [coolcode lang=”php”]
    < ?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();
    ?>
    [/coolcode]
    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;
    [coolcode lang=”php”]
    < ?php class LinkWidget { } class View { var $linkWidget; var $page; function View () { $this->linkWidget=new LinkWidget;
    }

    function renderPage () {
    $this->page=$this->linkWidget->display()
    }
    }
    ?>
    [/coolcode]
    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;
    [coolcode lang=”php”]
    < ?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); } } ?>
    [/coolcode]
    This would be represented as;

    The message is sent from View to HtmlUtils.

    Also messages could be sent both ways, such as;
    [coolcode lang=”php”]
    < ?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();
    ?>
    [/coolcode]
    [ Outputs: This is an error message ]

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

Resources

[tags] object management group, language uml, collaboration diagrams, model abstracts, statechart diagrams, class associations, case diagrams, oriented problem, umbrello, sequence diagrams, unified modelling language, modeling tools, unified modeling language, omg, php code, software systems, inheritance, deployment, scratch, real world[/tags]

3 Comments

  1. Actually in PHP5 third sample isn’t working, you’ll recieve message “Using $this when not in object context” on 4 line.

  2. Pingback: Web 2.0 Announcer
  3. There is one tool for uml
    Sparx Systems – Enterprise Architect

Comments are closed.